# Baekjoon-14499-주사위 굴리기

N,M,x,y,K = map(int,input().split())

board = []
for _ in range(N):
line = list(map(int, input().split()))
board.append(line)
board = board[::-1]

class Dice:
def __init__(self,board,x,y):
# top, bottom, north, south, west, east 순서
self.board = board
self.N,self.M = len(self.board),len(self.board[0])
self.status = [0,1,2,3,4,5]
self.nums = [0,0,0,0,0,0]
self.x = self.N-x-1
self.y = y

def move(self, comm):
is_move = False
if comm == 1 and self.y+1<M: # east
tmp = [self.status[i] for i in [4,5,2,3,1,0]]
is_move = True
self.y += 1
elif comm == 2 and self.y-1>=0: # west
tmp = [self.status[i] for i in [5,4,2,3,0,1]]
is_move = True
self.y -= 1
elif comm == 3 and self.x+1<N: # north
tmp = [self.status[i] for i in [3,2,0,1,4,5]]
is_move = True
self.x += 1
elif comm == 4 and self.x-1>=0: # south
tmp = [self.status[i] for i in [2,3,1,0,4,5]]
is_move = True
self.x -= 1

if is_move:
self.status = tmp
if self.board[self.x][self.y] == 0:
self.board[self.x][self.y] = self.nums[self.status[1]]
else:
self.nums[self.status[1]] = self.board[self.x][self.y]
self.board[self.x][self.y] = 0
print(self.top())

def top(self):
return self.nums[self.status[0]]

comms = list(map(int,input().split()))
dice = Dice(board, x,y)
for comm in comms:
dice.move(comm)