# 탈주범 검거 - SWEA

탈주범 검거 - SWEA

from collections import deque

def way(x, y):
    if a[x][y] == 1:
        w = [(1, 0), (-1, 0), (0, -1), (0, 1)]
    elif a[x][y] == 2:
        w = [(1, 0), (-1, 0)]
    elif a[x][y] == 3:
        w = [(0, -1), (0, 1)]
    elif a[x][y] == 4:
        w = [(-1, 0), (0, 1)]
    elif a[x][y] == 5:
        w = [(1, 0), (0, 1)]
    elif a[x][y] == 6:
        w = [(1, 0), (0, -1)]
    elif a[x][y] == 7:
        w = [(-1, 0), (0, -1)]
    elif a[x][y] == 0:
        w = []
    return w

T = int(input())

for test_case in range(1, T + 1):
    N, M, R, C, L = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(N)]
    visit = [[0] * M for _ in range(N)]
    cnt = 1

    q = deque()
    q.append((R, C))
    visit[R][C] = 1

    while q:
        x, y = q.popleft()
        for tx, ty in way(x, y):
            nx = x + tx
            ny = y + ty
            if nx < 0 or ny < 0 or nx >= N or ny >= M:
                continue
            if (-tx, -ty) not in way(nx, ny):
                continue
            if a[nx][ny] and not visit[nx][ny]:
                visit[nx][ny] = visit[x][y] + 1
                q.append((nx, ny))
                if visit[nx][ny] <= L:
                    cnt += 1

    print("#" + str(test_case) + " " + str(cnt))