Water filling problem in Blocks - Algebra Question Consider a rectangular plot comprising $n\times m$ square cells on which $nm$ cement blocks of various heights are stored, one block per cell. The base of each block covers one cell completely, and blocks on adjacent cells are so close that there are no gaps between them. Due to a heavy rain on the construction, water is accumulated in the gaps created between the blocks due to the difference in their heights. 
The question is how much water remains after the rain is over.

The question is in 2 parts with different measures of cubes and block area
Q A) Plot Breadth = 3
Plot Length = 6
Plot Block height
3 3 4
4 4 2
3 1 3
2 1 4
7 3 1
6 4 1

Answer is written as 5.
Q B)
Plot Breadth = 6
Plot Length = 3
3 3 7 3 1 3
4 3 1 4 2 6
4 1 4 2 4 1

Answer 5
I tried solving it by choosing the minimum difference in heights but my answer is not equal anything that is given. For the first problem my answer seems to be 2 and it is given as 5. :S
Would appreciate if anyone could help 
 A: You Can solve this problem inn this way.but we dont sure about complexity of the code
public static void main(String args[]) {
    int inputArray[] = { 3, 3, 4, 4, 4, 2, 3, 1, 3, 2, 1, 4, 7, 3, 1, 6, 4,
            1 };
    int totalWater = GetWaterLevel(3, 6, inputArray);
    System.out.println("Total Water=" + totalWater);
}

static int prows;
static int pcolumns;
static int pheightsArray[];

static boolean hasWall(int row, int column, int atHeight, int directionIndex) {
    if (row == 0 || row == prows - 1 || column == 0
            || column == pcolumns - 1)
        return false; // boundary check
    if (directionIndex == 1) { // up
        if (pheightsArray[(row - 1) * pcolumns + column] > atHeight)
            return true;
        else
            return (hasWall(row - 1, column, atHeight, 1)
                    && hasWall(row - 1, column, atHeight, 3) && hasWall(
                        row - 1, column, atHeight, 4));
    } else if (directionIndex == 2) { // down
        if (pheightsArray[(row + 1) * pcolumns + column] > atHeight)
            return true;
        else
            return (hasWall(row + 1, column, atHeight, 2)
                    && hasWall(row + 1, column, atHeight, 3) && hasWall(
                        row + 1, column, atHeight, 4));
    } else if (directionIndex == 3) { // left
        if (pheightsArray[row * pcolumns + column - 1] > atHeight)
            return true;
        else
            return (hasWall(row, column - 1, atHeight, 1)
                    && hasWall(row, column - 1, atHeight, 2) && hasWall(
                        row, column - 1, atHeight, 3));
    } else if (directionIndex == 4) { // right
        if (pheightsArray[row * pcolumns + column + 1] > atHeight)
            return true;
        else
            return (hasWall(row, column + 1, atHeight, 1)
                    && hasWall(row, column + 1, atHeight, 2) && hasWall(
                        row, column + 1, atHeight, 4));
    }
    return false;
}

public static int GetWaterLevel(int input1, int input2, int input3[]) {
    // Write code here
    int rows = input1;
    int columns = input2;
    int heightsArray[] = input3;
    prows = rows;
    pcolumns = columns;
    pheightsArray = heightsArray;
    int total = 0;
    int row = 0;
    int column = 0;
    int maxHeight = 0;
    int atHeight = 0;
    for (row = 0; row < rows * columns; row++)
        if (input3[row] > maxHeight)
            maxHeight = input3[row];
    for (row = 0; row < rows; row++) {
        for (column = 0; column < columns; column++) {
            // check if cell on boundaries
            if (row == 0 || row == rows - 1 || column == 0
                    || column == columns - 1)
                continue;
            // for each cell, rise from the cell's top to the max height
            // possible(can be optimized)
            // if at any height, cell is not walled, break loop for cell
            for (atHeight = heightsArray[row * columns + column]; atHeight < maxHeight; atHeight++) {
                if (hasWall(row, column, atHeight, 1) && // up
                        hasWall(row, column, atHeight, 2) && // down
                        hasWall(row, column, atHeight, 3) && // left
                        hasWall(row, column, atHeight, 4)) { // right
                    total++;
                } else
                    atHeight = maxHeight; // break height loop for cell
            }
        }
    }
    return total;
}

