Codeforces Round 771 (Div. 2)
D
Big Brush
Codeforces
2000
原题
← CE →
翻译中…

You found a painting on a canvas of size $n \times m$. The canvas can be represented as a grid with $n$ rows and $m$ columns. Each cell has some color. Cell $(i, j)$ has color $c_{i,j}$.

翻译中…

Near the painting you also found a brush in the shape of a $2 \times 2$ square, so the canvas was surely painted in the following way: initially, no cell was painted. Then, the following painting operation has been performed some number of times:

翻译中…
  • Choose two integers $i$ and $j$ ($1 \le i \lt n$, $1 \le j \lt m$) and some color $k$ ($1 \le k \le nm$).
  • Paint cells $(i, j)$, $(i + 1, j)$, $(i, j + 1)$, $(i + 1, j + 1)$ in color $k$.
翻译中…

All cells must be painted at least once. A cell can be painted multiple times. In this case, its final color will be the last one.

翻译中…

Find any sequence of at most $nm$ operations that could have led to the painting you found or state that it's impossible.

翻译中…

Input

翻译中…

The first line of input contains two integers $n$ and $m$ ($2 \le n, m \le 1000$) — the dimensions of the canvas.

翻译中…

On the $i$-th of the next $n$ lines of input, there will be $m$ integers. The $j$-th of them is $a_{i,j}$ ($1 \le a_{i,j} \le nm$) — the color of cell $(i, j)$.

翻译中…

Output

翻译中…

If there is no solution, print a single integer $-1$.

翻译中…

Otherwise, on the first line, print one integer $q$ ($1 \le q \le nm$) — the number of operations.

翻译中…

Next, print the operations in order. On the $k$-th of the next $q$ lines, print three integers $i$, $j$, $c$ ($1 \le i \lt n$, $1 \le j \lt m$, $1 \le c \le nm$) — the description of the $k$-th operation.

翻译中…

If there are multiple solutions, print any.

翻译中…

Note

翻译中…

In the first test case, the solution is not unique. Here's one of them:

翻译中…

image

翻译中…

In the second test case, there is no way one could obtain the given painting, thus the answer is $-1$.

翻译中…

样例

Input #1
4 4
5 5 3 3
1 1 5 3
2 2 5 4
2 2 4 4
Output #1
6
1 3 3
3 3 4
2 2 5
1 1 5
2 1 1
3 1 2
InputCopy OutputCopy
Input #2
3 4
1 1 1 1
2 2 3 1
2 2 1 1
Output #2
-1
InputCopy OutputCopy
题目信息
题号
1638D
时间限制
3 s
内存限制
256 MB
标签
构造
数据结构
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。