#ABC277F. [ABC277F] Sorting a Matrix

[ABC277F] Sorting a Matrix

Score : 500500 points

Problem Statement

You are given a matrix AA whose elements are non-negative integers. For a pair of integers (i,j)(i, j) such that 1iH1 \leq i \leq H and 1jW1 \leq j \leq W, let Ai,jA_{i, j} denote the element at the ii-th row and jj-th column of AA.

Let us perform the following procedure on AA.

  • First, replace each element of AA that is 00 with an arbitrary positive integer (if multiple elements are 00, they may be replaced with different positive integers).
  • Then, repeat performing one of the two operations below, which may be chosen each time, as many times as desired (possibly zero).
    • Choose a pair of integers (i,j)(i, j) such that 1i<jH1 \leq i \lt j \leq H and swap the ii-th and jj-th rows of AA.
    • Choose a pair of integers (i,j)(i, j) such that 1i<jW1 \leq i \lt j \leq W and swap the ii-th and jj-th columns of AA.
  • Choose a pair of integers (i,j)(i, j) such that 1i<jH1 \leq i \lt j \leq H and swap the ii-th and jj-th rows of AA.
  • Choose a pair of integers (i,j)(i, j) such that 1i<jW1 \leq i \lt j \leq W and swap the ii-th and jj-th columns of AA.

Determine whether AA can be made to satisfy the following condition.

  • $A_{1, 1} \leq A_{1, 2} \leq \cdots \leq A_{1, W} \leq A_{2, 1} \leq A_{2, 2} \leq \cdots \leq A_{2, W} \leq A_{3, 1} \leq \cdots \leq A_{H, 1} \leq A_{H, 2} \leq \cdots \leq A_{H, W}$.
  • In other words, for every two pairs of integers (i,j)(i, j) and (i,j)(i', j') such that 1i,iH1 \leq i, i' \leq H and 1j,jW1 \leq j, j' \leq W, both of the following conditions are satisfied.
    • If i<ii \lt i', then Ai,jAi,jA_{i, j} \leq A_{i', j'}.
    • If i=ii = i' and j<jj \lt j', then Ai,jAi,jA_{i, j} \leq A_{i', j'}.
  • If i<ii \lt i', then Ai,jAi,jA_{i, j} \leq A_{i', j'}.
  • If i=ii = i' and j<jj \lt j', then Ai,jAi,jA_{i, j} \leq A_{i', j'}.

Constraints

  • 2H,W2 \leq H, W
  • H×W106H \times W \leq 10^6
  • 0Ai,jH×W0 \leq A_{i, j} \leq H \times W
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

HH WW

A1,1A_{1, 1} A1,2A_{1, 2} \ldots A1,WA_{1, W}

A2,1A_{2, 1} A2,2A_{2, 2} \ldots A2,WA_{2, W}

\vdots

AH,1A_{H, 1} AH,2A_{H, 2} \ldots AH,WA_{H, W}

Output

If AA can be made to satisfy the condition in the problem statement, print Yes; otherwise, print No.

3 3
9 6 0
0 4 0
3 0 3
Yes

One can perform the operations as follows to make AA satisfy the condition in the problem statement, so you should print Yes.

  • First, replace the elements of AA that are 00, as shown below:
9 6 8
5 4 4
3 1 3
  • Swap the second and third columns. Then, AA becomes:
9 8 6
5 4 4
3 3 1
  • Swap the first and third rows. Then, AA becomes:
3 3 1
5 4 4
9 8 6
  • Swap the first and third columns. Then, AA becomes the following and satisfies the condition in the problem statement.
1 3 3
4 4 5
6 8 9
2 2
2 1
1 2
No

There is no way to perform the operations to make AA satisfy the condition in the problem statement, so you should print No.