This page is still under construction.

Parts of this page are still being built. What you see may change.

Binary Matrix

Time limit5sMemory limit128 MB

Summary
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 r×cr \times c. Every entry of the matrix is 00 or 11. In one operation you may flip a single entry: a 00 becomes 11, and a 11 becomes 00. By performing this operation any number of times, you want to obtain a matrix that satisfies both of the following conditions.

  1. Every row contains the same number of 11s.
  2. Every column contains the same number of 11s.

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 TT (T≤1000T \le 1000).

For each test case, the first line contains two integers rr and cc (1≤r,c≤401 \le r, c \le 40), where rr is the number of rows and cc is the number of columns. Each of the next rr lines contains cc 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 11) 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 −1-1 instead of R.

Examples1

  1. Example 1

    Input
    3
    2 3
    111
    111
    3 3
    011
    011
    011
    2 3
    001
    000
    
    Expected output
    Case 1: 0
    Case 2: 3
    Case 3: 1