Street light poles

Given a binary r by c grid, find the minimum number of cell flips so every row has equal pole count and every column has equal pole count, or -1 if impossible.

Medium4ImplementationMathInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

A city has rr horizontal streets and cc vertical streets. The two directions cross and form an r×cr \times c grid, and every cell of the grid either holds one light pole (1) or is empty (0). One operation places a light pole on an empty cell, or removes the light pole from a cell that has one.

Change the grid so that both of these conditions hold:

  1. Every horizontal street has the same number of light poles.
  2. Every vertical street has the same number of light poles.

Compute the minimum number of operations needed to reach both conditions.

Input

The first line has the number of test cases TT. (1T10001 \le T \le 1000)

The first line of each test case has the number of horizontal streets rr and the number of vertical streets cc, separated by a space. (1r,c401 \le r, c \le 40)

The next rr lines each hold a string of length cc made only of the characters 0 and 1. If the jj-th character of the ii-th line is 1, the cell where horizontal street ii meets vertical street jj has a light pole. If it is 0, that cell is empty.

Output

For each test case, print one line in the format Case i: R. Here ii is the test case number starting from 1, and RR is the minimum number of operations needed to reach both conditions. If no grid satisfies the conditions, print -1 in place of RR.