You are given a grid consisting of rows and columns, containing only of s and s.
You can do the following operations on :
- Select a row () and invert it (i.e. flip every value in that row, such that each becomes a and each becomes a ).
- Select a column () and invert it.
A binary grid is beautiful if there are no three consecutive equal values in the same row or in the same column. More formally, there is no , (, ) such that , and there is no , () such that .
Task
Your task is to decide whether it is possible to make a given grid beautiful, and if so, then report the minimum number of operations to do it.
Input data
The first line of the input file contains a single integer , the number of testcases. testcases follow, each preceded by an empty line.
Each testcase consists of:
- a line containing two space-separated integers and .
- lines, the -th of which consisting of string consisting of s and s representing row of the grid.
Output data
The output file must contain lines, each consisting of a single integer, the answers of the testcases. If it is possible to make a grid beautiful, then the answer to the testcase is the minimum number of operations to do so, otherwise, if it is impossible, then the answer is .
Constraints and clarifications
- is either or for each and .
- The sum of over all testcases is at most .
- The sum of over all testcases is at most .
# | Points | Constraints |
---|---|---|
1 | 0 | Examples |
2 | 9 | and . The sum of and the sum of over all testcases do not exceed . |
3 | 12 | |
4 | 20 | . The sum of over all testcases does not exceed . |
5 | 59 | No additional constraints |
Example
stdin
3
4 4
0001
1110
1010
1000
3 3
011
101
110
5 5
11111
10001
11011
10001
11111
stdout
3
0
-1
Explanation
Explanation of the first sample:
In the first testcase, a possible way to make the grid beautiful using operations is as follows:
- Invert column .
- Invert row .
- Invert column .
In the second testcase, the grid is already beautiful, thus no operations are needed.
In the third testcase, it is impossible to make the grid beautiful using the mentioned operations.