/*
1030 시작
1시간 컷
1. 비지트로 하고, 할 때마다 답을 +1 을 합니다.
2. 갈 수 있는 길을 알아보는 것,
지금 시작 위치가 3이기때문에 왼쪽, 오른쪽을 검사하고, 갈 수 있으면 비지트를 합니다.
왼쪽이 무엇이면ㅇ
*/
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define SIZE 55 // 55
int N, M, L, R, C;
int map[SIZE][SIZE];
int visit[SIZE][SIZE];
int ans;
int num;
int sec = 1;
struct point {
int x, y;
};
queue<point> que;
int x, y, nx, ny;
void problemIn() {
cin >> N >> M >> R >> C >> L;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> map[i][j];
}
}
}
void init() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
map[i][j] = 0;
visit[i][j] = 0;
}
}
while (!que.empty()) que.pop();
sec = 1;
ans = 0;
}
void bfs() {
while (!que.empty()) {
int qlen = que.size();
if (sec == L) {
return;
}
while (qlen--) {
x = que.front().x;
y = que.front().y;
num = map[x][y];
que.pop();
if (num == 1) { // 위, 아래, 오른쪽, 왼쪽
if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
if (visit[x - 1][y] == 0) {
visit[x - 1][y] = 1;
ans++;
que.push({ x - 1, y });
}
}
if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
if (visit[x + 1][y] == 0) {
visit[x + 1][y] = 1;
ans++;
que.push({ x + 1, y });
}
}
if (map[x][y + 1] == 1 || map[x][y + 1] == 6 || map[x][y + 1] == 7 || map[x][y + 1] == 3) {
if (visit[x][y + 1] == 0) {
visit[x][y + 1] = 1;
ans++;
que.push({ x, y + 1 });
}
}
if (map[x][y - 1] == 1 || map[x][y - 1] == 3 || map[x][y - 1] == 4 || map[x][y - 1] == 5) {
if (visit[x][y - 1] == 0) {
visit[x][y - 1] = 1;
ans++;
que.push({ x, y - 1 });
}
}
}
else if (num == 2) { // 위, 아래
if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
if (visit[x - 1][y] == 0) {
visit[x - 1][y] = 1;
ans++;
que.push({ x - 1, y });
}
}
if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
if (visit[x + 1][y] == 0) {
visit[x + 1][y] = 1;
ans++;
que.push({ x + 1, y });
}
}
}
else if (num == 3) { // 오른쪽, 왼쪽
if (map[x][y + 1] == 1 || map[x][y + 1] == 6 || map[x][y + 1] == 7 || map[x][y + 1] == 3) {
if (visit[x][y + 1] == 0) {
visit[x][y + 1] = 1;
ans++;
que.push({ x, y + 1 });
}
}
if (map[x][y - 1] == 1 || map[x][y - 1] == 3 || map[x][y - 1] == 4 || map[x][y - 1] == 5) {
if (visit[x][y - 1] == 0) {
visit[x][y - 1] = 1;
ans++;
que.push({ x, y - 1 });
}
}
}
else if (num == 4) { // 위, 오른쪽
if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
if (visit[x - 1][y] == 0) {
visit[x - 1][y] = 1;
ans++;
que.push({ x - 1, y });
}
}
if (map[x][y + 1] == 1 || map[x][y + 1] == 6 || map[x][y + 1] == 7 || map[x][y + 1] == 3) {
if (visit[x][y + 1] == 0) {
visit[x][y + 1] = 1;
ans++;
que.push({ x, y + 1 });
}
}
}
else if (num == 5) { // 오른쪽, 아래
if (map[x][y + 1] == 1 || map[x][y + 1] == 6 || map[x][y + 1] == 7 || map[x][y + 1] == 3) {
if (visit[x][y + 1] == 0) {
visit[x][y + 1] = 1;
ans++;
que.push({ x, y + 1 });
}
}
if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
if (visit[x + 1][y] == 0) {
visit[x + 1][y] = 1;
ans++;
que.push({ x + 1, y });
}
}
}
else if (num == 6) { // 왼쪽, 아래
if (map[x][y - 1] == 1 || map[x][y - 1] == 3 || map[x][y - 1] == 4 || map[x][y - 1] == 5) {
if (visit[x][y - 1] == 0) {
visit[x][y - 1] = 1;
ans++;
que.push({ x, y - 1 });
}
}
if (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) {
if (visit[x + 1][y] == 0) {
visit[x + 1][y] = 1;
ans++;
que.push({ x + 1, y });
}
}
}
else if (num == 7) { // 왼쪽, 위에
if (map[x][y - 1] == 1 || map[x][y - 1] == 3 || map[x][y - 1] == 4 || map[x][y - 1] == 5) {
if (visit[x][y - 1] == 0) {
visit[x][y - 1] = 1;
ans++;
que.push({ x, y - 1 });
}
}
if (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) {
if (visit[x - 1][y] == 0) {
visit[x - 1][y] = 1;
ans++;
que.push({ x - 1, y });
}
}
} // end else if
} // end qlen
// if (sec == (L-1)) {
// return;
// }
sec++;
} // end que.empty
}
void solve() {
visit[R][C] = 1;
ans++;
que.push({ R,C });
bfs();
}
int main() {
int T;
cin >> T;
for (int tc = 1; tc <= T; tc++) {
problemIn();
solve();
cout << "#" << tc << " " << ans << endl;
init();
}
return 0;
}