problems / 283

Suki's Lookout Mounds

easy matrix arrays 1000 ms · 256 MB

Suki's Lookout Mounds

Suki, a black curly-coated dog with a grey beard and white paws, loves lying on top of the dirt mounds she has dug in the garden, because from up there she can watch the whole street. The garden is a grid of R rows and C columns of square patches, and every patch has a height.

A patch makes a good lookout if it is strictly higher than each of its neighbours: the patches directly above, below, to the left and to the right of it that lie inside the garden. Diagonal patches don't count. A patch on the edge of the garden has fewer neighbours, and in a garden of a single patch that patch has none at all, so it is a lookout.

Help Suki find all her lookouts.

Input

The first line contains two integers R and C (1 ≤ R, C ≤ 500).

Each of the next R lines contains C integers h_ij (0 ≤ h_ij ≤ 10^9): the height of the patch in row i, column j. Rows are numbered from 1 top to bottom and columns from 1 left to right.

Output

On the first line print k, the number of lookouts. Then print k lines, each with two integers: the row and the column of a lookout. List them in row-major order: by row, and within a row by column, both increasing. If there are no lookouts, print only 0.

Notes

In the first example the patch of height 9 in row 2, column 2 is higher than its four neighbours 5, 6, 7 and 2. The corner patch of height 8 has only two neighbours, 3 and 7, and is higher than both, and so is the corner patch of height 7 in row 3, column 1 (neighbours 6 and 2). The patch of height 7 in row 2, column 3 is not a lookout, because its neighbour 9 is higher: being higher than its other neighbours isn't enough.

In the second example every patch has the same height. A patch must be strictly higher than its neighbours, so there are no lookouts.

Example 1

Input

3 4
1 5 3 8
6 9 7 2
7 2 1 0

Output

3
1 4
2 2
3 1

Example 2

Input

2 3
4 4 4
4 4 4

Output

0

Hints · reveal one at a time if you're stuck

Hint 1

You don't need anything clever: for each patch, just look at the patches above, below, left and right of it. How do you know whether one of them is outside the garden?

Hint 2

A neighbour outside the garden doesn't exist, so it can't block the view: skip it rather than giving it some made-up height. Watch for ties too: is a patch next to one of equal height a lookout? If you visit the patches in the right order, you won't need to sort the answers.

report an issue Prefer your own editor? sooks prep 283 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.