Checkerboard Matrix (Large)
Time limit5sMemory limit512 MB
Find the fewest row and column swaps that turn the given binary matrix into a checkerboard, or report IMPOSSIBLE.
Problem
When she is bored, Mija plays with matrices. She tries to turn one matrix into another using as few moves as possible. For Mija, one move is swapping two rows of the matrix or swapping two columns of the matrix.
Today Mija has a very special matrix . is a matrix whose entries are all 0 or 1. Mija wants to turn into a checkerboard matrix, a matrix whose entries alternate between 0 and 1 along every row and along every column. Find the minimum number of moves that turns into a checkerboard matrix.
Input
The first line contains the number of test cases . test cases follow. Each test case starts with a line containing a single integer . The next lines each contain characters, which are the rows of . Each character is 0 or 1.
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the minimum number of row swaps and column swaps that turns into a checkerboard matrix. If cannot be turned into a checkerboard matrix, print IMPOSSIBLE in place of .
Hint
In the first sample case, is already a checkerboard matrix.
In the second sample case, Mija swaps columns 1 and 2 and then swaps rows 1 and 2 to get a checkerboard matrix.
In the third sample case, no sequence of moves works because the matrix does not have enough ones.