#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
char g[N][N];
bool st[N][N];
int T1, n, m, T, cnt;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
void exchange(int x, int y, int z){
if(z == 1){
g[x][y] = '1';
}else{
g[x][y] = '0';
}
}
void bfs(int sx, int sy){
queue<PII> q;
st[sx][sy] =true;
q.push(make_pair(sx, sy));
while(q.size()){
PII t = q.front();
q.pop();
for(int i = 0; i < 4;++ i){
int x = t.x + dx[i], y = t.y + dy[i];
if(x < 0 || x >= n || y < 0 || y >= m) continue;
if(g[x][y] != '1') continue;
if(st[x][y]) continue;
q.push(make_pair(x, y));
st[x][y] = true;
}
}
}
int main(){
cin >> T1;
int temp = 1;
while(T1 --){
cin >> n >> m;
for(int i = 0;i < n; ++i){
for(int j = 0;j < m; ++j){
cin >> g[i][j];
}
}
cout <<"Case #" << temp ++ << ":" <<endl;
cin >> T;
while(T --){
char test;
cin >> test;
if(test == 'M'){
int x,y,z;
cin >> x >> y >> z;
exchange(x, y, z);
}else{
for(int i = 0;i < n; ++i){
for(int j = 0;j < m; ++j){
if(g[i][j] == '1' && st[i][j] == false){
cnt ++;
bfs(i,j);
}
}
}
cout << cnt << endl;
cnt = 0;
memset(st, false, sizeof st);
}
}
}
return 0;
}