#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int t, n, m, k, x, y, d, ans;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
char mp[N][N];
int vis[N][N];
int main() {
cin >> t;
while(t--) {
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 >> mp[i][j];
}
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
while(k--) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && mp[x][y] == '.') {
x = nx;
y = ny;
if (vis[x][y] == 0) {
ans++;
vis[x][y] = 1;
}
} else {
d = (d + 1) % 4;
}
}
cout << ans << endl;
}
return 0;
}