
Primonimo is a game played on an n×m board. Every square holds an integer between 1 and p, where p is a prime. One move picks a square, and every square in the same row or the same column as the picked square grows by 1. The picked square belongs to both that row and that column, but it still grows by only 1. A square that held p goes back to 1.
You win when every square shows p. Given the starting board, find a sequence of moves that wins the game.
Squares are numbered from 1 to nm in row-major order, so the square in row i and column j (both counted from 1) has number (i−1)m+j.
The input is one test case. The first line has three integers n, m, p: the number of rows n (1≤n≤20), the number of columns m (1≤m≤20), and a prime p (2≤p≤97). Each of the next n lines has m integers between 1 and p.
If no winning sequence exists, print −1.
Otherwise the answer is fixed by this rule. The order of the moves does not change the result, so only the number of times each square is picked matters. Let xt be the number of times square t is picked, and consider only the count vectors with 0≤xt≤p−1. When several count vectors win, take the lexicographically smallest (x1,x2,…,xnm): the smallest x1, then among those the smallest x2, and so on.
Print k=x1+⋯+xnm on the first line. On the second line print the k chosen square numbers from the smallest number up, writing number t exactly xt times. When k=0 the second line is empty. This k is always at most p⋅n⋅m.