Given an N x M grid of an island where grid[i][j] is the height of cell (i, j). Left and top sides of the grid are an ocean, right and down sides of the grid are a river. When it rains over a cell, the cell is full of water and the water spreads to all neighbouring cells with height <= the height of the current cell. The water continues to spread till it cannot move any more. Count the cells that if rains starts over it, the river and the ocean will be connected.
Example:
Input: grid = [[1, 2, 3, 2], [2, 3, 3, 2], [3, 3, 3, 2], [2, 4, 4, 1]]
Output: 12
Explanation: If it rains over any of cells (0, 0), (0, 1) or (1, 0) the water will connect the ocean with itself.
If it rains over cell (3, 3), the water will connect the river with itself.
Otherwise, if it rains over any of the 12 other cells, the water will spread to connect the ocean with the river.