#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
#define M 105
char state[M][M];
int flag[M][M];
int table[M][M];
int adj[16]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int rows,cols;
typedef struct
{
int x;
int y;
}point;
queue <point> myqueue;
queue <point> copy_queue;
void find_point(int &x,int &y,string str)
{
int i,s_flag = 0;
int len = str.length();
for(i=0; i < len; i++)
{
if(str[i]==' ')
{
s_flag = 1;
continue;
}
else if(s_flag==0)
{
x = x*10+str[i]-'0';
}
else if(s_flag == 1)
{
y = y*10+str[i]-'0';
}
}
}
bool out(int x,int y)
{
if(x<0||y<0||x>rows||y>cols)
return false;
else
return true;
}
void bfs(int x,int y)
{
int ans=0;
int xx,yy;
point p;
flag[x][y] = 1;
p.x = x;
p.y = y;
myqueue.push(p);
copy_queue.push(p);
while(!myqueue.empty())
{
p = myqueue.front();
ans++;
x = p.x;
y = p.y;
for(int i=0; i<16; i+=2)
{
xx = x+adj[i];
yy = y+adj[i+1];
if(out(xx,yy)&&flag[xx][yy]==0&&state[xx][yy]=='W')
{
flag[xx][yy] = 1;
p.x = xx;
p.y = yy;
myqueue.push(p);
copy_queue.push(p);
}
}
myqueue.pop();
}
while(!copy_queue.empty())
{
p = copy_queue.front();
table[p.x][p.y] = ans;
copy_queue.pop();
}
}
void cal_wet()
{
int i,j;
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
if(flag[i][j]==0&&state[i][j]=='W')
{
bfs(i,j);
flag[i][j] = 1;
}
}
}
}
void input()
{
int i,x,y;
int s_flag;
string str;
rows = 0;
cols = 0;
s_flag = 0;
while(1)
{
getline(cin,str);
x = 0;
y = 0;
if(str.length()==0)
{
rows = 0;
memset(flag,0,sizeof(flag));
break;
}
if(isdigit(str[0]))
{
if(s_flag==0)
{
cal_wet();
s_flag = 1;
}
find_point(x,y,str);
cout<<table[x-1][y-1]<<endl;
}
else if(str[0]=='L'||str[0]=='W')
{
cols = str.length();
for(i=0; i<cols; i++)
{
state[rows][i] = str[i];
}
rows++;
}
if(cin.eof())
break;
}
}
int main()
{
int num;
int ans;
char ch;
string str;
cin>>num;
ch = cin.get();
getline(cin,str);
while(num--)
{
ans = 0;
memset(flag,0,sizeof(flag));
memset(state,0,sizeof(state));
memset(table,0,sizeof(table));
input();
if(num)
cout<<endl;
if(cin.eof())
break;
}
return 0;
}