#include <iostream>
#include <cstring>
using namespace std;
char a[1145][1145];
bool b[1145][1145];
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int main() {
int t;
cin >> t;
while (t--) {
int n, m, k, x, y, d;
cin >> n >> m >> k;
cin >> x >> y >> d;
int c = 1;
memset(b, 0, sizeof(b));
memset(a, 0, sizeof(a));
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
cin >> a[i][j];
}
}
while (k--) {
int lx = x + dx[d], ly = y + dy[d];
if (a[lx][ly] == '.' && b[lx][ly] == false) {
++c;
x = lx;
y = ly;
}
else {
d = (d + 1) % 4;
}
}
cout << c << endl;
}
}