#C1242. Pacific Atlantic Water Flow

    ID: 41845 Type: Default 1000ms 256MiB

Pacific Atlantic Water Flow

Pacific Atlantic Water Flow

You are given an n x m grid where each cell contains an integer representing the elevation at that point. Water can flow from a cell to its neighboring cells (up, down, left, or right) if and only if the neighboring cell's elevation is equal or lower than the current cell's elevation. The Pacific Ocean touches the top and left edges of the grid, while the Atlantic Ocean touches the bottom and right edges.

A cell is considered valid if water starting from that cell can flow to both the Pacific and Atlantic oceans. Mathematically, if we denote the height of the current cell as \(h_{current}\) and an adjacent cell as \(h_{next}\), then water can flow from the current cell to the next cell if: \[ h_{next} \ge h_{current} \]

Your task is to compute all valid cell coordinates (row and column indices) from which water can reach both oceans. There is no required order for the output.

inputFormat

The input is read from standard input. The first line contains two integers \(n\) and \(m\), representing the number of rows and columns of the grid, respectively. This is followed by \(n\) lines, each containing \(m\) space-separated integers representing the terrain elevations.

outputFormat

For each cell that can allow water to flow to both oceans, print a line containing two integers: the row index and the column index of the cell, separated by a space. The order of the output cells does not matter.

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

1 3 1 4 2 2 3 0 3 1 4 0

</p>