Make the matrix happy!
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Today everyone is celebrating Happy Numbers Day!
Zenik and Marichka found an interesting activity for themselves today.
You are given an \(n \times m\) matrix where each element is a decimal digit (from 0 to 9). Marichka and Zenik take turns choosing a row or a column of this matrix and replacing all digits in it with the next ones. A matrix consisting only of the digits 4 and 7 is called happy. Check whether Zenik and Marichka can obtain a happy matrix if they have an unlimited number of operations.
The definition of the next digit \(next(c)\) for a digit \(c\) is as follows:
\[next(c) = \begin{cases} c+1 & \quad \text{if } c < 9\\ 0 & \quad \text{if } c = 9 \end{cases}\]
Input
The first line contains two integers \(n\) and \(m\) – the dimensions of the matrix.
Each of the next \(n\) lines contains \(m\) integers separated by spaces – the elements of the matrix.
Output
In a single line, print YES if it is possible to obtain
a happy matrix. Otherwise, print NO.
Constraints
\(1 \le n, m \le 1000\),
\(0 \le a_{ij} \le 9\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 3 9 5 4 0 3 5 4 7 3 | YES |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 3 8 5 4 0 3 5 4 7 3 | NO |
Notes
For the first example, we apply the following operations: the first column three times, the third column, the first row twice, the second row.
We get the matrix:
\(\begin{pmatrix} 4 & 7 & 7 \\ 4 & 4 & 7 \\ 7 & 7 & 4 \end{pmatrix}\)
In the second example, it is impossible to achieve a happy matrix.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|