LearnPy Hub
← Arena
AdvancedRating 1600

Water Reaching Both Oceans

Rain flows from a cell to a neighbour (up, down, left, right) of equal or lower height. The Pacific touches the top and left edges, the Atlantic the bottom and right edges. Count the cells from which water can reach both oceans.

Input

r and c, then r rows of heights.

Output

One integer.

Sample 1

Input
5 5
1 2 2 3 5
3 2 3 4 4
2 4 5 3 1
6 7 1 4 5
5 1 1 2 4
Output
7

Sample 2

Input
1 1
1
Output
1
solution.py
Output appears here
Water Reaching Both Oceans · Python Arena problem · LearnPy Hub