eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Binary Matrix 2

Binary Matrix 2

Лимит времени 5 секунд
Лимит использования памяти 64 MiB

You are given a matrix of size r×c. Each of the elements can be either 0 or 1. In each operation you can flip any element of this matrix, i.e. convert 0 to 1 or convert 1 to 0. Your goal is to convert the matrix such that -

  1. Each of the rows will have the same number of 1s and

  2. Each of the columns will have the same number of 1s.

What is the minimum number of operations required to achieve this?

Входные данные

Input starts with a positive integer T1000) which indicates the number of inputs.

Each case starts with two integers m and n (1r, c40), here r is the number of rows and c is the number of columns of the matrix. Each of the next m lines will have n integers each, either 0 or 1.

Выходные данные

For each test case, output "Case #: R" in a single line, where # will be replaced by case number and R will be replaced by the minimum number of steps required to achieve the target matrix. Replace R by -1 if it is not possible to reach target matrix.

Пример

Входные данные #1
3
2 3
111
111
3 3
011
011
011
2 3
001
000
Выходные данные #1
Case 1: 0
Case 2: 3
Case 3: 1
Источник ACM-ICPC Asia Hatyai Regional Programming Contest – November 16, 2012 – PSU, Hatyai