#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 20;
const int maxs = (1 << 16) + 10;
int wp[maxn], cankill[maxs];
ll d[maxs];
char s[maxn];
int main()
{
int T;
scanf("%d", &T);
int cas=0;
while(T--) {
int n;
scanf("%d", &n);
for(int i = 0; i <= n; i++) {
scanf("%s", s);
wp[i] = 0;
for(int j = 0; j < n; j++) {
if(s[j]-'0') wp[i] |= (1<<j);
}
}
int nState = (1 << n) - 1;
cankill[0] = wp[0];
for(int i = 1; i <= nState; i++) {
cankill[i] = wp[0];
for(int j = 0; j < n; j++) {
if((1 << j) & i) cankill[i] |= wp[j + 1];
}
}
memset(d, 0, sizeof(d));
d[0] = 1;
for(int s = 1; s <= nState; s++) {
for(int j = 0; j < n; j++) {
if(((1 << j) & s) && (cankill[s ^ (1 << j)] & (1 << j))) {
d[s] += d[s ^ (1 << j)];
}
}
}
printf("Case %d: %lld\n", ++cas, d[nState]);
}
return 0;
}