#P2919. Hilltops
Hilltops
Hilltops
Farmer John owns a farm with many hills, and he wishes to place a guard at the top of each hill to protect his valuable milk-cows. The farm is represented by a matrix of integers denoting altitudes. You are given a matrix with $N$ rows and $M$ columns where each element $H_{ij}$ (with $0 \le H_{ij} \le 10000$) represents the altitude at position $(i,j)$. Two elements are considered adjacent if the absolute difference of their row indices is at most 1 and the absolute difference of their column indices is also at most 1.
A hilltop is defined as one or more adjacent cells of equal altitude that are surrounded exclusively by cells with a strictly lower altitude or by the edge of the matrix. Your task is to determine the number of hilltops on the map.
Note: If a plateau touches the boundary of the map, it is still considered a hilltop provided that every adjacent cell (that is not part of the plateau) has a lower altitude.
inputFormat
The input starts with a line containing two integers $N$ and $M$ separated by a space ($1 < N \le 700$, $1 < M \le 700$). The next $N$ lines each contain $M$ integers, representing the altitude values of the map. Each integer $H_{ij}$ satisfies $0 \le H_{ij} \le 10000$.
outputFormat
Output a single integer representing the number of hilltops on the map.
sample
8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0
5