Binary Matrix
Time limit5sMemory limit128 MB
Given a binary matrix, flip the fewest entries so every row has the same number of 1s and every column has the same number of 1s, or report that it is impossible.
- Level
Medium6 of 10
- Topics
- Greedy, Combinatorics, Implementation, Math
- Solved
- No attempts yet
Problem
You are given a matrix of size . Every entry of the matrix is or . In one operation you may flip a single entry: a becomes , and a becomes . By performing this operation any number of times, you want to obtain a matrix that satisfies both of the following conditions.
- Every row contains the same number of s.
- Every column contains the same number of s.
Compute the minimum number of operations needed to turn the given matrix into a matrix that satisfies both conditions.
Input
The first line contains the number of test cases ().
For each test case, the first line contains two integers and (), where is the number of rows and is the number of columns. Each of the next lines contains digits with no spaces, describing one row of the matrix.
Output
For each test case, print one line in the form Case #: R, where # is the test case number (starting from ) and R is the minimum number of operations needed to transform the given matrix into one that satisfies the conditions. If it is impossible, print instead of R.