Karen and Game
Contest: Codeforces Round 419 (Div. 1)
Difficulty rating: 1700
Tags: brute force, greedy, implementation

On the way to school, Karen became fixated on the puzzle game on her phone!

The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.

One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.

To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to gi, j.

Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!

Input format:
The first line of input contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively.

The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains gi, j (0 ≤ gi, j ≤ 500).

Output format:
If there is an error and it is actually not possible to beat the level, output a single integer -1.

Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level.

The next k lines should each contain one of the following, describing the moves in the order they must be done:

- row x, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".
- col x, (1 ≤ x ≤ m) describing a move of the form "choose the x-th column".

If there are multiple optimal solutions, output any one of them.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level:

In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.

In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level:

Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.

Example input:
3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1

Example output:
4
row 1
row 1
col 4
row 3

Example input:
3 3
0 0 0
0 1 0
0 0 0

Example output:
-1

Editorial:
Fix the number of times we choose the first row. Say we choose the first row k times. This actually uniquely determines the rest of the solution; consider the cells on the first row. There is no other way to increase these cells, except by choosing the columns they are on, and so we need to choose the j-th column g1, j - k times.
Now, once we know the number of times we have to choose each column, we will also know how many times to choose the remaining rows. At this point, for any given row i, the remaining number of times we have to choose it is gi, j - (g1, j - k), this should be the same for all j in a given row (otherwise there is no solution).
We can simply try all k, see if they can form a valid solution, and if so, calculate how many moves it will take. Find the smallest required number of moves, and then recover the solution.
Implemented properly, it should run in O(n) time, which should be fast enough. Note that there are O(max gi, j) possible choices for k, and testing a certain k can be done in O(nm) time. A solution will have at most O(max(n, m)·max gi, j) moves, so printing them will take that much time.
Overall, this solution hence runs in O(nm·max gi, j) time, which is acceptable.
There is a faster solution to this, both in terms of runtime and implementation time, which we will describe below.
Notice that, when there is a 0 on the grid, all the moves are already fixed. If the 0 is at gi, j, then we need to choose row i' exactly gi', j times, and column j' exactly gi, j' times.
What if there is no 0 on the grid? Well, we intuitively want to reduce numbers as much as possible, and in fact the greedy algorithm works here. If there are not more rows (n ≤ m), we should keep choosing rows, and if there are fewer columns (n > m), we should keep choosing columns, until there is a 0. It doesn't even matter which particular rows or columns we choose; for example, if n ≤ m, we could just keep choosing row 1 until a 0 appears, or we could choose all rows 1, 2, 3, ..., n in order and just keep cycling through them. The end result will be the same.
We can just check that the grid is correct at the end and print  - 1 otherwise.
Implemented properly, this runs in O(nm + max(n, m)·max gi, j), which is asymptotically optimal; it takes at least O(nm) time to read the input, and O(max(n, m)·max gi, j) to print the output.