#include <bits/stdc++.h>
using namespace std;
struct robot
{
long x = 0;
long y = 0;
int d = 0;
};
int solution();
int main()
{
int T = 0;
cin >> T;
cin.get();
for (int i = 0; i < T; i++)
{
long posNum = solution();
cout << posNum << endl;
}
}
int solution()
{
int n,m;
long k;
robot robot;
cin >> n >> m >> k;
cin.get();
char map[n][m];
vector <string> have_been;
cin >> robot.x >> robot.y >> robot.d;
cin.get();
for (int x = 0; x < n; x++)
{
for (int y = 0; y < m; y++)
{
map[x][y] = cin.get();
}
cin.get();
}
have_been.push_back(to_string(robot.x) + ',' + to_string(robot.y));
for (long steps = 0; steps < k; steps++)
{
long next_position[2]{robot.x,robot.y};
switch (robot.d)
{
case 0:
next_position[1] += 1;
break;
case 1:
next_position[0] += 1;
break;
case 2:
next_position[1] += -1;
break;
case 3:
next_position[0] += -1;
break;
default:
break;
}
if (next_position[0] >= 1 && next_position[0] <= n && next_position[1] >= 1 && next_position[1] <= m &&
map[next_position[0]-1][next_position[1]-1] == '.')
{
robot.x = next_position[0];
robot.y = next_position[1];
string nowpos = to_string(robot.x) +','+ to_string(robot.y);
have_been.push_back(nowpos);
}else{
robot.d = (robot.d + 1) % 4;
}
}
sort(have_been.begin(),have_been.end());
have_been.erase(unique(have_been.begin(),have_been.end()),have_been.end());
if (have_been.size() == 1) return 1;
return have_been.size();
}