/*
这道题其实很简单,就是卡在了初始化的时候,如果只在查到M是进行初始化,那么如果两个Q是挨着的那么第二次
查询的时候就没有初始化。
题目大致思路:其实最核心的就是进行Q的查询,首先遍历所有没有标记过的1,然后进行bfs标记处于这个1连通的所有1
完了之后,这就是一个连通集,那么这样就可记录连通集的数量。
*/
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 110;
int a[N][N];
bool st[N][N];
int r,c,n;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
void bfs(int sx,int sy) {
queue<PII> q;
q.push({sx,sy});
st[sx][sy] = true;
while(!q.empty()) {
auto t = q.front();q.pop();
for(int i = 0;i < 4;i++) {
int ax = t.x + dx[i],ay = t.y + dy[i];
if(ax >= 0 && ax < r && ay >= 0 && ay < c && !st[ax][ay] && a[ax][ay] == 1) {
st[ax][ay] = true;
q.push({ax,ay});
}
}
}
}
int main (){
int T;
scanf("%d",&T);
for(int cases = 1;cases <= T;cases++) {
scanf("%d%d",&r,&c);
for(int i = 0;i < r;i++) {
for(int j = 0;j < c;j++) {
scanf("%01d",&a[i][j]);
}
}
printf("Case #%d:\n",cases);
scanf("%d",&n);
while(n--) {
char ch;
getchar();
scanf("%c",&ch);
if(ch == 'Q') {
int res = 0;
// 卡在初始化标记数组好久 必须是在查到Q之前进行初始化,都是多组数据的错
memset(st,false,sizeof st);
for(int i = 0;i < r;i++) {
for(int j = 0;j < c;j++) {
if(!st[i][j] && a[i][j] == 1) {
bfs(i,j);
res ++;
}
}
}
printf("%d\n",res);
}else{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[x][y] = z;
}
}
}
return 0;
}