#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
char Map[N][N];
bool vis[N][N];
int main() {
int T;
int n, m, k, x0, y0, d0, ans, x, y, d;
cin >> T;
while (T--) {
memset(vis, 0, sizeof(vis));
cin >> n >> m >> k >> x0 >> y0 >> d0;
for (int i = 1; i <= n; i++) {
string a;
cin >> a;
for (int j = 1; j <= m; j++)
Map[i][j] = a[j - 1];
}
x = x0, y = y0, d = d0;
while (k--) {
ans = 0;
vis[x][y] = 1;
if (d == 0) {
if (Map[x][y + 1] == '.')
y++, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
if (d == 1) {
if (Map[x + 1][y] == '.')
x++, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
if (d == 2) {
if (Map[x][y - 1] == '.')
y--, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
if (d == 3) {
if (Map[x - 1][y] == '.')
x--, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans += vis[i][j];
cout << ans << endl;
}
return 0;
}