#include <iostream>
#include <vector>
#include <unordered_set>
#include <string>
using namespace std;
struct Position {
int x, y;
bool operator==(const Position& other) const {
return x == other.x && y == other.y;
}
};
struct PositionHash {
size_t operator()(const Position& pos) const {
return hash<int>()(pos.x) * 31 + hash<int>()(pos.y);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, m, k;
cin >> n >> m >> k;
int x0, y0, d0;
cin >> x0 >> y0 >> d0;
vector<string> grid(n);
for (int i = 0; i < n; ++i) {
cin >> grid[i];
}
unordered_set<Position, PositionHash> visited;
Position current = {x0, y0};
visited.insert(current);
auto move_forward = [&](int& dx, int& dy) -> bool {
int nx = current.x + dx;
int ny = current.y + dy;
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && grid[nx - 1][ny - 1] == '.') {
current = {nx, ny};
return true;
}
return false;
};
int dx[] = {0, 1, 0, -1}; // east, south, west, north
int dy[] = {1, 0, -1, 0};
int d = d0;
for (int i = 0; i < k; ++i) {
if (!move_forward(dx[d], dy[d])) {
d = (d + 1) % 4; // turn right
} else {
visited.insert(current);
}
}
cout << visited.size() << "\n";
}
return 0;
}