#include<bits/stdc++.h>
using namespace std;
int t,n,m,k,x,y,d,ans;
char s[1005][1005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>t;
for(int i=1;i<=t;i++) {
ans=1;
cin>>n>>m>>k>>x>>y>>d;
for(int j=1;j<=n;j++)
for(int l=1;l<=m;l++)
cin>>s[j][l];
for(int j=1;j<=k;j++) {
if(d==0) {
if(s[x][y+1]=='.'&&y+1<=m) {
y++;
ans++;
}
else
d=(d+1)%4;
}
else if(d==1) {
if(s[x+1][y]=='.'&&x+1<=n) {
x++;
ans++;
}
else
d=(d+1)%4;
}
else if(d==2) {
if(s[x][y-1]=='.'&&y-1>=1) {
y--;
ans++;
}
else
d=(d+1)%4;
}
else if(d==3) {
if(s[x-1][y]=='.'&&x-1>=1) {
x--;
ans++;
}
else
d=(d+1)%4;
}
}
cout<<ans<<"\n";
}
return 0;
}