#C11759. Count Peaks in a Grid

    ID: 41110 Type: Default 1000ms 256MiB

Count Peaks in a Grid

Count Peaks in a Grid

In this problem, you are given a grid of integers with ( n ) rows and ( m ) columns. A cell ( (i,j) ) is considered a peak if its value is strictly greater than the value of its four adjacent neighbors (up, down, left, and right). In other words, a cell ( grid_{i,j} ) is a peak if:

$$grid_{i,j} > grid_{i-1,j}, \quad grid_{i,j} > grid_{i+1,j}, \quad grid_{i,j} > grid_{i,j-1}, \quad grid_{i,j} > grid_{i,j+1} $$

Note that boundary cells have fewer than four neighbors, and comparisons are only made with existing adjacent cells. Your task is to count the number of peak cells in the grid.

Input is provided via standard input (stdin), and the resulting count should be printed to standard output (stdout).

inputFormat

The first line contains two integers ( n ) and ( m ) (separated by a space) representing the number of rows and columns, respectively. This is followed by ( n ) lines, each containing ( m ) integers separated by spaces, which represent the grid.

outputFormat

Output a single integer representing the number of peak cells in the grid, printed on a single line.## sample

4 4
1 2 1 3
4 1 5 6
7 8 2 4
5 9 1 2
3

</p>