#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 15 + 2,INF = 1e9 + 7;
int A[maxn][maxn], B[maxn][maxn],n;
int Check(int flag) {
memset(B, 0, sizeof(B));
for (int i = 1; i <= n; i++) {
if (flag & (1 << (i - 1))) B[1][i] = 1;
else if (A[1][i] == 1) return INF;
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int sum = B[i - 2][j] + B[i - 1][j - 1] + B[i - 1][j + 1];
B[i][j] = sum & 1;
if (A[i][j] == 1 && B[i][j] == 0) return INF;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (A[i][j] != B[i][j]) ++cnt;
}
}
return cnt;
}
int main()
{
int t,len;
cin >> t;
for (int kCase = 1; kCase <= t; kCase++) {
cin >> n;
memset(A, 0, sizeof(A));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> A[i][j];
}
}
len = 1 << n;
int ans = INF;
for (int i = 0; i < len; i++) {
ans = min(Check(i),ans);
}
if (ans == INF) ans = -1;
cout << "Case " << kCase << ": " << ans <<endl;
}
return 0;
}
|