#include <bits/stdc++.h>
using namespace std;
int T;
int n,m,k;
int x,y,d;
int ans = 1;
char forest[1005][1005],f[1005][1005];
int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
void solve()
{
memset(forest,' ',sizeof(forest));
memset(f,0,sizeof(f));
ans = 1;
cin >> n >> m >> k;
cin >> x >> y >> d;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
cin >> forest[i][j];
while (k--)
{
int xx = x + dx[d];
int yy = y + dy[d];
if (forest[xx][yy] == '.' && xx >=1 && xx <= n && y >= 1 && y <= m)
{
x = xx;
y = yy;
if (f[x][y] == 0)
ans++;
f[x][y] = 1;
}
else
{
d = (d + 1) % 4;
}
}
cout << ans << endl;
}
int main()
{
cin >> T;
while (T--)
{
solve();
}
return 0;
}