#include<bits/stdc++.h>
using namespace std;
char str[1003][1003];
int len,counter;
bool check[103][103];
void dfs(int i,int j)
{
if(i<0||j<0||i>=len||str[i][j]=='L'||str[i][j]=='\0')
{
return ;
}
if(check[i][j]==true)
{
return ;
}
if(str[i][j]=='W' && check[i][j]==false)
{
counter++;
check[i][j] = true;
dfs(i+1,j);
dfs(i-1,j);
dfs(i,j+1);
dfs(i,j-1);
dfs(i-1,j+1);
dfs(i-1,j-1);
dfs(i+1,j-1);
dfs(i+1,j+1);
}
}
int main()
{
int test,i,j,a,b,x;
scanf("%d",&test);
getchar();
getchar();
for(x=1; x<=test; x++)
{
if(x>1)printf("\n");
j=0;
bool flag = true;
while(gets(str[j]))
{
if(str[j][0]=='\0')break;
if(str[j][0]>='0' && str[j][0]<='9')
{
if(flag)
{
flag = false;
len = j ;
}
sscanf(str[j],"%d %d",&a,&b);
counter=0;
dfs(a-1,b-1);
printf("%d\n",counter);
memset(check,false,sizeof check);
}
j++;
}
memset(str,'\0',sizeof str);
}
return 0 ;
}