#K33807. Pacific Atlantic Water Flow

    ID: 25169 Type: Default 1000ms 256MiB

Pacific Atlantic Water Flow

Pacific Atlantic Water Flow

Given an m x n matrix of non‐negative integers representing the height of each cell in a continent, water can flow from a cell to any adjacent cell (up, down, left, or right) with a height equal to or lower than the current cell. The Pacific Ocean touches the left and top edges of the matrix, and the Atlantic Ocean touches the right and bottom edges.

Your task is to identify all coordinates (r, c) from which water can flow to both the Pacific and Atlantic oceans.

Note: If a cell is on the border of an ocean, water is assumed to flow directly into that ocean.

LaTeX Formula: Two cells (i, j) and (k, l) are adjacent if and only if \(|i-k| + |j-l| = 1\).

inputFormat

The first line contains two integers m and n representing the number of rows and columns of the matrix. Each of the following m lines contains n integers separated by spaces representing the height values of the cells.

outputFormat

Output the coordinates of all cells from which water can flow to both the Pacific and Atlantic oceans. Each coordinate should be printed on a separate line as two integers (row and column) separated by a space. The order of the coordinates 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>