//
// Created by 16327 on 2024/11/17.
//
#include <bits/stdc++.h>
using namespace std;
vector<vector<char>> arr(1005,vector<char>(1005));
int main(){
int T,n,m,k,count=1;
char temp;
int x,y,d;
cin >> T;
while(T--){
cin >> n >> m >> k;
cin >> x >> y >> d;
x--,y--;
//地图
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin >> temp;
arr[i][j] = temp;
}
}
// for(int i=0;i<n;i++)
// for(int j=0;j<m;j++)
// cout << arr[i][j];
for(int i=0;i<k;i++) {
if (d == 0) {
if (arr[x][y + 1] == '.') {
y += 1;
count++;
continue;
}
} else {
d = (d + 1) % 4;
continue;
}
if (d == 1) {
if (arr[x + 1][y] == '.') {
x += 1;
count++;
continue;
}
} else {
d = (d + 1) % 4;
continue;
}
if (d == 2) {
if (arr[x][y - 1] == '.') {
y -= 1;
count++;
continue;
}
} else {
d = (d + 1) % 4;
continue;
}
if (d == 3) {
if (arr[x - 1][y] == '.') {
y -= 1;
count++;
continue;
}
} else {
d = (d + 1) % 4;
continue;
}
}
cout << count << endl;
count = 1;
}
return 0;
}