#include<bits/stdc++.h>
using namespace std;
void f()
{
int n,m,k,x,y,d,x1,y1,sum=0;
char a[100][100];
cin>>n>>m>>k>>x>>y>>d;
x1=x;
y1=y;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=1;i<=k;i++)
{
if(d==0)
{
if(a[x][y+1]!='X' and x<n and x>1 and y<m and y>1) y=y+1;
else
{
d++;
d=d%4;
}
}
else if(d==2)
{
if(a[x][y-1]!='X' and x<n and x>1 and y<m and y>1) y=y-1;
else
{
d++;
d=d%4;
}
}
else if(d==1)
{
if(a[x+1][y]!='X' and x<n and x>1 and y<m and y>1) x=x+1;
else
{
d++;
d=d%4;
}
}
else if(d==3)
{
if(a[x-1][y]!='X' and x<n and x>1 and y<m and y>1) x=x-1;
else
{
d++;
d=d%4;
}
}
//if(a[x][y]!='#') sum++;
a[x][y]='#';
}
for(int i=1;i<=x1;i++)
for(int j=1;j<=y1;j++)
if(a[i][j]=='#') sum++;
cout<<sum<<endl;
return ;
}
int main()
{
int t;
cin>>t;
while(t--)
f();
}