#C10282. Counting Peaks in a City Map

    ID: 39470 Type: Default 1000ms 256MiB

Counting Peaks in a City Map

Counting Peaks in a City Map

You are given a grid representing a city map where each cell contains a building height. Your task is to count the number of peaks in the grid. A building is considered a peak if its height is greater than or equal to the heights of all its adjacent neighbors. The neighbors include the eight surrounding cells (i.e. horizontally, vertically, and diagonally adjacent cells).

In mathematical terms, for a building located at position \( (i,j) \) with height \( h_{i,j} \), it is a peak if and only if for every neighbor \( (i+\Delta i, j+\Delta j) \) (with \( \Delta i, \Delta j \in \{-1, 0, 1\} \) and not both zero), either the neighbor is out-of-bound or \( h_{i,j} \ge h_{i+\Delta i,j+\Delta j} \).

Your program should read the grid from standard input and output the number of peaks to standard output.

inputFormat

The first line contains two integers \( n \) and \( m \) where \( n \) is the number of rows and \( m \) is the number of columns of the grid. Each of the next \( n \) lines contains \( m \) space-separated integers representing the building heights in the grid.

outputFormat

Output a single integer which is the number of peaks in the grid.

## sample
3 3
1 2 1
2 3 2
1 2 1
1