Problem F
Sign of a Matrix
Input: Standard Input

Output: Standard Output

 

You have a n x n zero matrix. In each operation, you can add one (or minus 1) to every element of a whole row, or add one (or minus 1) to every element of a whole column. Given the target signs of every element of the matrix, how many operations are needed?

 

Input

There will be at most 100 test cases. Each test case begins with a line containing a single integer n (2 ≤ n ≤ 100), followed by n lines of n characters in each line. Each character is one of +, - or 0, corresponding to positive, negative and zero, respectively.

 
Output
For each test case, print the case number and the minimum number of operations needed. If the target cannot be reached, print -1.

 

Sample Input                            Output for Sample Input

4
0+00
-+--
0+00
0+00
2
+0
00

-1

Case 1: 3

Case 2: -1

 

Sample elaboration:

For the first sample input, target can be achieved by 3 moves only. By increasing the second column twice and decreasing the second row once. Which will convert the initial matrix to the following-

 0 +2  0  0
-1 +1 -1 -1
 0 +2  0  0
 0 +2  0  0

Which is the target matrix.


Problemsetter: Rujia Liu

Refurbished by: Sohel Hafiz

Special Thanks: Arifuzzaman Arif