#include<bits/stdc++.h>
#define gc getchar()
#define pc(a) putchar(a)
#define isd(a) (a>='0'&&a<='9')
#define writeln(a) write(a,'\n')
using namespace std;
template<typename T>
void read(T&r)
{
r=0;char ch=gc,last='z';
while(!isd(ch)) last=ch,ch=gc;
while(isd(ch)) r=(r<<1)+(r<<3)+(ch^48),ch=gc;
if(last=='-') r=-r;
}
int _buf[100],_len;
template<typename T>
void write(T w,char endc=' ')
{
_len=0;
if(w<0) pc('-'),w=-w;
do _buf[_len++]=w%10; while(w/=10);
for(int i=_len-1;i>=0;i--) pc(_buf[i]+'0');
if(endc!=0) pc(endc);
}
#define turn(x,y) (m*((x)-1)+(y))
#define nturn(x) (_y=((x)-1)%m+1,_x=((x)-_y)/m+1)
struct Node{
int px,py,ch;
};
vector<Node> path[800001];
int n,m,q;
char buf[800001];
struct user{
int col,lv;
bool wk;
user():col(0),lv(0),wk(false){}
};
user us[801001];
int col,lv,x,y;
struct Point{
int x,y,ch;
int lx,ly;
};
queue<Point> que;
int _x,_y;
bool vis[801001];
bool vs[801001][4];
int from(int lx,int ly,int x,int y)
{
if(lx-x==1) return 0;
if(x-lx==1) return 1;
if(ly-y==1) return 2;
if(y-ly==1) return 3;
return -1;
}
int bfs()
{
memset(vis,0,sizeof(bool)*(n+2)*(m+2));
memset(vs,0,sizeof(bool)*(n+2)*(m+2)*5);
int tot=0;
que.push((Point){x,y,-1,-1,-1});
vis[turn(x,y)]=true;
while(!que.empty())
{
Point p=que.front();que.pop();
// printf("%d %d %d %d %d\n",p.ch,p.lx,p.ly,p.x,p.y);
// int nx=p.x,ny=p.y,nch=p.ch;
int tur=turn(p.x,p.y);
if(vs[tur][from(p.lx,p.ly,p.x,p.y)]) continue;
vs[tur][from(p.lx,p.ly,p.x,p.y)]=true;
if(!vis[tur]) tot++,vis[tur]=true;
if(p.ch==1) continue;
// printf("debug: %d\n",path[tur].size());
for(int i=0;i<(int)path[tur].size();i++)
{
if(p.ch!=-1&&(p.ch!=path[tur][i].ch)) continue;
if(path[tur][i].px==p.lx&&path[tur][i].py==p.ly) continue;
if(p.ch==2&&(p.x-p.lx!=path[tur][i].px-p.x||p.y-p.ly!=path[tur][i].py-p.y)) continue;
// printf("kkksc03: %d %d %d\n",path[tur][i].px,path[tur][i].py,path[tur][i].ch);
if(!us[turn(path[tur][i].px,path[tur][i].py)].wk) que.push((Point){path[tur][i].px,path[tur][i].py,path[tur][i].ch,p.x,p.y});
else
{
if(us[turn(path[tur][i].px,path[tur][i].py)].col==col)
{
// if(!vis[turn(path[tur][i].px,path[tur][i].py)])
// tot++,vis[turn(path[tur][i].px,path[tur][i].py)]=true;
}
else if(us[turn(path[tur][i].px,path[tur][i].py)].lv<=lv)
{
if(!vis[turn(path[tur][i].px,path[tur][i].py)])
tot++,vis[turn(path[tur][i].px,path[tur][i].py)]=true;
}
}
}
}
for(int i=1;i<=n*m;i++)
if(vis[i])
{
nturn(i);
// printf("%d %d\n",_x,_y);
}
return tot;
}
int main()
{
int T;
read(T);
while(T--)
{
read(n),read(m),read(q);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
path[turn(i,j)].clear();
memset(us,0,sizeof(user)*(n+2)*(m+2));
for(int i=1;i<=n;i++)
{
scanf("%s",buf+1);
for(int j=1;j<m;j++)
if(buf[j]!='0')
path[turn(i,j)].push_back((Node){i,j+1,buf[j]-'0'}),
path[turn(i,j+1)].push_back((Node){i,j,buf[j]-'0'});
}
for(int i=1;i<n;i++)
{
scanf("%s",buf+1);
for(int j=1;j<=m;j++)
if(buf[j]!='0')
path[turn(i,j)].push_back((Node){i+1,j,buf[j]-'0'}),
path[turn(i+1,j)].push_back((Node){i,j,buf[j]-'0'});
}
// puts("OK>");
while(q--)
{
read(col),read(lv),read(x),read(y);
writeln(bfs());
us[turn(x,y)].wk=true;
us[turn(x,y)].col=col;
us[turn(x,y)].lv=lv;
// if(q%1000==0) writeln(q);
}
// system("start calc");
//hhh
}
return 0;
}
//Big moni
//CCF is playing again!
0 sc.