Potatoes in the basement
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Winter is coming. Potato supplies have long been stored in the basement. It should be enough for the whole winter. The plan is as follows: Zenik carries potatoes from the basement to the kitchen, Marichka cooks, and the cat sits importantly and watches everything carefully. Today we will help Zenik, because he got the hardest job.
If you look at the basement from above, it looks like a rectangle of height \(h\) cells and width \(w\) cells. In each cell of the basement there are boxes of potatoes — cubes of size \(1 \times 1 \times 1\), stacked on top of each other. In the \(j\)-th cell of the \(i\)-th row there are \(a_{i j}\) boxes stacked on top of each other. At a time, Zenik can take away exactly one box from the basement. It is clear that this must be one of the boxes that is on top, meaning nothing is on top of it.
In order for the cat to be satisfied with Zenik’s work, the potato storage in the basement must always remain beautiful. The potato storage in the basement is called beautiful if the number of boxes in each cell of the basement does not exceed the number of boxes in the cell to the left and above it. In other words, the numbers of boxes must form a non-increasing sequence in each row and each column of the basement.
Zenik is very good at carrying boxes of potatoes. Your task is to help him not upset the cat.
It is necessary to state in what order Zenik can take the boxes from the basement so that the potato storage remains beautiful all the time. Note that the winter is planned to be long, so Zenik will have to take all the boxes from the storage.
Input
The first line contains two integers \(h\) and \(w\) — the height and width of the basement.
The following \(h\) lines are given with \(w\) integers \(a_{i j}\) each — the initial quantities of boxes in each cell.
Output
In the first line, print an integer \(c\) — the number of boxes of potatoes in the basement.
In the next \(c\) lines, print triples of numbers \(x\), \(y\), \(z\) — the order in which Zenik should take the boxes. \(x\), \(y\), \(z\) means that the next box to take is at the intersection of row \(x\) and column \(y\), which is the \(z\)-th in order in this cell. Rows and columns are 1-indexed, and boxes in the same cell are 1-indexed starting from the bottom.
If there are several possible sequences of boxes, print any of them.
Constraints
\(1 \le h, w \le 100\),
\(0 \le a_{i j} \le 100\),
it is guaranteed that the initial storage is beautiful.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 2 3 2 1 1 1 0 0 | 5 1 1 2 1 3 1 2 1 1 1 2 1 1 1 1 |
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 |
|---|