#include <iostream>
#include <cstring>
using namespace std;
bool vis[1005][1005];
char mp[1005][1005];
int main()
{
int T;
cin >> T;
while(T--)
{
memset(vis, 0, sizeof vis);
int n, m, k;
cin >> n >> m >> k;
int x, y, d;
cin >> x >> y >> d;
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> mp[i][j];
for(int i = 1; i <= k; i++)
{
int nx = x, ny = y;
if(d == 0) ny++;
if(d == 1) nx++;
if(d == 2) ny--;
if(d == 3) nx--;
if(1 <= nx && nx <= n && 1 <= ny && ny <= m && mp[nx][ny] == '.')
{
vis[nx][ny] = 1;
x = nx, y = ny;
}
else d = (d + 1) % 4;
}
int cnt = 1;
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cnt += vis[i][j];
cout << cnt << endl;
}
return 0;
}