1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX = 16; 7 const int x[] = { -1, -1, -1, 0, 1, 1, 1, 0 }; 8 const int y[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; 9 10 char arr[MAX][MAX]; 11 bool vis[MAX][MAX]; 12 13 void DFS(int a, int b, int n) 14 { 15 int ta, tb; 16 if(vis[a][b]) 17 { 18 vis[a][b] = false; 19 } 20 if(arr[a][b] != '0') 21 { 22 return; 23 } 24 for(int i = 0; i < 8; i++) 25 { 26 ta = a + x[i]; tb = b + y[i]; 27 if((ta >= 0 && ta < n) && (tb >= 0 && tb < n) && vis[ta][tb]) 28 { 29 DFS(ta, tb, n); 30 } 31 } 32 } 33 34 int main() 35 { 36 #ifdef CDZSC_OFFLINE 37 freopen("in.txt", "r", stdin); 38 freopen("out.txt", "w", stdout); 39 #endif 40 int t, n, kase = 0; 41 scanf("%d", &t); 42 while(t--) 43 { 44 memset(arr, 0, sizeof(arr)); 45 memset(vis, true, sizeof(vis)); 46 scanf("%d", &n); 47 for(int i = 0; i < n; i++) 48 { 49 scanf("%s", arr[i]); 50 } 51 int ans = 0; 52 for(int i = 0; i < n; i++) 53 { 54 for(int j = 0; j < n; j++) 55 { 56 if(arr[i][j] == '@') 57 { 58 vis[i][j] = false; 59 } 60 } 61 } 62 for(int i = 0; i < n; i++) 63 { 64 for(int j = 0; j < n; j++) 65 { 66 if(vis[i][j] && arr[i][j] == '0') 67 { 68 DFS(i, j, n); 69 ans++; 70 } 71 } 72 } 73 for(int i = 0; i < n; i++) 74 { 75 for(int j = 0; j < n; j++) 76 { 77 if(vis[i][j] && arr[i][j] != '@') 78 ans++; 79 } 80 } 81 printf("Case %d: %d ", ++kase, ans); 82 } 83 return 0; 84 }