#include<bits/stdc++.h>
#define int long long
#define N 1005
using namespace std;
char a[N][N];
bool vis[N][N];
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T;
cin>>T;
while(T--)
{
memset(vis,0,sizeof(vis));
int n,m,k,x,y,d,i,j,cnt=0,dx,dy;
cin>>n>>m>>k>>x>>y>>d;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++) a[i][j]=getchar();
getchar();
}
for(i=1;i<=k;i++)
{
vis[x][y]=1;
dx=0,dy=0;
if(d==0) dy++;
else if(d==1) dx++;
else if(d==2) dy--;
else dx--;
if(x+dx<=n&&y+dy<=m&&x+dx>0&&y+dy>0&&a[x+dx][y+dy]=='.') x+=dx,y+=dy;
else d=(d+1)%4;
}
for(i=1;i<=n;i++) for(j=1;j<=m;j++) cnt+=vis[i][j];
cout<<cnt<<endl;
}
return 0;
}