#include<bits/stdc++.h>
using namespace std;
int t,n,m,k;
int x,y,d;
char s[1005][1005];
int dy[40]={1,0,-1,0},dx[40]={0,1,0,-1};
int main(){
cin>>t;
for(int l=1;l<=t;l++){
memset(s,0,sizeof(s));
int num=1;
cin>>n>>m>>k;
cin>>x>>y>>d;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>s[i][j];
while(k>0){
if(s[x+dx[d]][y+dy[d]]=='.'){
x=x+dx[d];
y=y+dy[d];
num++;
}
else{
d=(d+1)%4;
}
k--;
}
cout<<num<<'\n';
}
return 0;
}
比赛时忘删调试导致爆0的也是我?