#d1701. 画画
画画
Painting
Problem Description
There is now an canvas. Each cell has a colour represented by a positive integer ().
You may perform the following operation at most times on a blank canvas:
Choose a square on the canvas and paint the whole square with any colour ().
Every cell must be painted at least once. A cell may be painted multiple times; in that case its final colour is the last colour applied.
Now determine whether there exists a sequence of operations that makes your final canvas identical to the given canvas . If no valid painting plan exists, output -1.
Input Format
The first line contains two integers and (), the dimensions of the canvas.
The following lines each contain integers; the -th integer of the -th line is (), the colour of each cell.
Output Format
If there is no solution, output one line containing the integer -1.
Otherwise, output an integer () on the first line, the number of operations.
Then output the operations in order, lines, each containing three integers : the top-left corner of your chosen square () and the colour that square will be painted.
If there are multiple solutions, output any of them.
Sample Input 1
4 4
5 5 3 3
1 1 5 3
2 2 5 4
2 2 4 4
Sample Output 1
6
1 3 3
3 3 4
2 2 5
1 1 5
2 1 1
3 1 2
Sample Input 2
3 4
1 1 1 1
2 2 3 1
2 2 1 1
Sample Output 2
-1
Sample Explanation
For the first input, the following is one valid painting plan:

For the second input, it is impossible to find a suitable solution.