#K35607. Peak Cells in Grids

    ID: 25569 Type: Default 1000ms 256MiB

Peak Cells in Grids

Peak Cells in Grids

You are given several grid datasets. Each grid is represented by its dimensions and a matrix of integers. Your task is to determine the number of peak cells in each grid.

A peak cell is defined as a cell such that none of its 8 neighboring cells (adjacent horizontally, vertically, or diagonally) has a strictly greater value than it. In other words, a cell at position \( (i,j) \) in an \( n \times m \) grid is a peak if for every neighbor \( (i+di, j+dj) \) (with \( di, dj \in \{-1,0,1\} \) and not both zero) that exists within the bounds of the grid, the following holds:

\( grid[i][j] \geq grid[i+di][j+dj] \)

You need to process multiple grids from the input and output the count of peak cells in each grid on a new line.

inputFormat

The input contains one or more grid datasets. Each dataset begins with a line containing two integers \( n \) and \( m \) (\( 1 \leq n, m \leq 1000 \)), representing the number of rows and columns in the grid. This is followed by \( n \) lines, each containing \( m \) space-separated integers representing the grid values. The input terminates with a line containing two zeros "0 0", which should not be processed.

outputFormat

For each grid, output a single line containing the number of peak cells in that grid.

## sample
3 3
1 2 1
2 3 2
1 2 1
4 4
5 3 4 7
3 8 6 5
4 6 5 3
7 4 3 2
2 2
1 2
3 4
0 0
1

3 1

</p>