N, M = map(int, input().split())
r, c, d = map(int, input().split())
mapC = []
for i in range(N):
mapC.append(list(map(int, input().split())))
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
def rotate(d):
if d == 0:
d = 3
else:
d -= 1
return d
mapC[r][c] = 2
result = 1
while True:
isChange = False
for i in range(4):
d = rotate(d)
nr = r + dx[d]
nc = c + dy[d]
if (0 <= nr < N) and (0 <= nc < M):
if mapC[nr][nc] == 0:
mapC[nr][nc] = 2
r = nr
c = nc
result += 1
isChange = True
break
if not isChange:
nr = r - dx[d]
nc = c - dy[d]
if (0 > nr or nr >= N) or (0 > nc or nc >= M):
break
if mapC[nr][nc] == 1:
break
else:
r, c = nr, nc
print(result)