#K7576. Peak Element Finder in a 2D Grid
Peak Element Finder in a 2D Grid
Peak Element Finder in a 2D Grid
You are given a 2D grid of integers. A peak element is defined as an element that is greater than or equal to each of its adjacent neighbors (neighbors in the four cardinal directions: up, down, left, and right). Your task is to find any one peak element from the grid.
In mathematical terms, if the grid is denoted as \(A\) with dimensions \(n \times m\), then an element \(A_{i,j}\) is a peak if it satisfies:
[ A_{i,j} \geq A_{i-1,j}, \quad A_{i,j} \geq A_{i+1,j}, \quad A_{i,j} \geq A_{i,j-1}, \quad A_{i,j} \geq A_{i,j+1} ]
Note that the elements on the boundary of the grid have fewer than four neighbors, and the condition applies only for the existing neighbors. It is guaranteed that the grid always contains at least one peak element.
Input is provided via stdin and the answer should be output via stdout.
inputFormat
The input is read from stdin. The first line contains two integers \(n\) and \(m\), representing the number of rows and columns in the grid. The following \(n\) lines each contain \(m\) space-separated integers representing the grid elements.
For example:
3 3 10 20 15 21 30 14 7 16 32
outputFormat
Output a single integer which is any one peak element from the grid. The output should be written to stdout.
## sample3 3
10 20 15
21 30 14
7 16 32
30
</p>