You are given an M x N binary matrix where each cell is either 0 or 1. This matrix wraps both in horizontally and vertically. So, ith row is adjacent to (i + 1)th row for all 1 ≤ i < M and Mth row is adjacent to 1st row. Similarly, ith column is adjacent to (i + 1)th column for all 1 ≤ i < N and Nth column is adjacent to 1st column. Obviously row a is adjacent to row b implies that row b is adjacent to row a, and same thing is true for columns. Now, two cells of this matrix are adjacent if they are in the same row and their columns are adjacent, or they are in the same column and their rows are adjacent. So for a 3 x 5 matrix, cell (2, 3) has 4 adjacent cells (1, 3), (2, 2), (2, 4), (3, 3) and cell (3, 5) has 4 adjacent cells (2, 5), (3, 4), (3, 1), (1, 5). Note that, by cell (i, j) we mean the cell of ith row and jth column.
You are only allowed to swap the values of any two adjacent cells of the matrix. Your task is to transform the matrix in such a way that, each of the rows has same number of ones and each of the columns has same number of ones. If it is possible print "both". If it is not possible try to make the rows having equal number of ones. If it is possible print "row". If it is also not possible try to make the columns having equal number of ones. If it is possible print "column". Also print the minimum number of swaps required. But if none of these is possible you have to print "impossible".
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with two integers M and N (2 ≤ M, N ≤ 1000), number of rows and columns of the matrix. Each of the next M lines contains N characters denoting the rows of the matrix. jth character of the ith line denotes the value of cell (i, j) of the matrix.
Output
For each case, print the case number and the result as specified.
Sample
Sample Input | Sample Output |
---|---|
3 2 3 001 111 2 4 0010 0111 2 2 00 01 | Case 1: row 1 Case 2: both 3 Case 3: impossible |
Notes
Dataset is huge, user faster I/O method.