atcoder#ABC224E. [ABC224E] Integers on Grid
[ABC224E] Integers on Grid
Score : points
Problem Statement
We have a grid with horizontal rows and vertical columns. Let denote the square at the -th row from the top and -th column from the left.
Each square contains an integer. For each , the square contains a positive integer . The other square contains a .
Initially, there is a piece on the square . Takahashi can move the piece to a square other than the square it occupies now, any number of times. However, when moving the piece, both of the following conditions must be satisfied.
- The integer written on the square to which the piece is moved is strictly greater than the integer written on the square from which the piece is moved.
- The squares to and from which the piece is moved are in the same row or the same column.
For each , print the maximum number of times Takahashi can move the piece when .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. For each , the -th line should contain the maximum number of times Takahashi can move the piece when .
3 3 7
1 1 4
1 2 7
2 1 3
2 3 5
3 1 2
3 2 5
3 3 5
1
0
2
0
3
1
0
The grid contains the following integers.
4 7 0
3 0 5
2 5 5
- When , you can move the piece once by moving it as .
- When , you cannot move the piece at all.
- When , you can move the piece twice by moving it as .
- When , you cannot move the piece at all.
- When , you can move the piece three times by moving it as $(3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2)$.
- When , you can move the piece once by moving it as .
- When , you cannot move the piece at all.
5 7 20
2 7 8
2 6 4
4 1 9
1 5 4
2 2 7
5 5 2
1 7 2
4 6 6
1 4 1
2 1 10
5 6 9
5 3 3
3 7 9
3 6 3
4 3 4
3 3 10
4 2 1
3 5 4
1 2 6
4 7 9
2
4
1
5
3
6
6
2
7
0
0
4
1
5
3
0
5
2
4
0