#K33422. Grid Area Calculator
Grid Area Calculator
Grid Area Calculator
You are given a grid of size \(n \times m\) where each cell contains an integer. A cell with the value \(-1\) represents an empty or blocked cell. For every other integer \(v\), its area is defined as the number of times \(v\) appears in the grid. That is, the area of \(v\) is given by:
[ Area(v) = \sum_{i=1}^{n} \sum_{j=1}^{m} [grid[i][j] = v] \quad \text{for} \quad v \neq -1 ]
Your task is to compute the area for each distinct integer (other than \(-1\)) and output the results sorted in ascending order of the integer.
inputFormat
The input is read from stdin and has the following format:
- The first line contains two integers \(n\) and \(m\), representing the number of rows and columns of the grid.
- The next \(n\) lines each contain \(m\) space-separated integers, representing the grid cells.
Each cell is either an integer (\(v\)) or \(-1\). Only values different from \(-1\) should be counted.
outputFormat
The output is written to stdout. For every distinct integer \(v\) (where \(v \neq -1\)) found in the grid, output a line containing \(v\) and its corresponding area (the count of \(v\) in the grid), separated by a space. The lines must be sorted in ascending order based on \(v\). If there are no valid numbers, do not output anything.
## sample4 5
-1 1 1 -1 -1
-1 1 1 2 2
-1 -1 -1 -1 2
3 3 -1 -1 2
1 4
2 4
3 2
</p>