import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st=new StringTokenizer(br.readLine());
n=Integer.parseInt(st.nextToken());
m=Integer.parseInt(st.nextToken());
y=Integer.parseInt(st.nextToken());
x=Integer.parseInt(st.nextToken());
k=Integer.parseInt(st.nextToken());
board=new int[n][m];
ud=new int[4];
lr=new int[4];
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<m;j++)board[i][j]=Integer.parseInt(st.nextToken());
}
st=new StringTokenizer(br.readLine());
for(int i=0;i<k;i++){
int dir=Integer.parseInt(st.nextToken())-1;
int dy=y+d[dir][0];
int dx=x+d[dir][1];
if(dy<0||dx<0||dy>=n||dx>=m)continue;
if(dir==0){
swapRight(lr,ud,dy,dx);
}
else if(dir==1){
swapLeft(lr,ud,dy,dx);
}
else if(dir==2){
swapLeft(ud,lr,dy,dx);
}
else{
swapRight(ud,lr,dy,dx);
}
y=dy;
x=dx;
}
}
static int n,m,x,y,k;
static int[][] d=new int[][]{{0,1},{0,-1},{-1,0},{1,0}};
static int[][] board;
static int[] ud,lr;
static void swapLeft(int[] arr,int[] other,int y,int x){
int a=arr[0];
for(int i=1;i<4;i++){
arr[i-1]=arr[i];
}
arr[3]=a;
if(board[y][x]==0){
board[y][x]=arr[3];
}
else{
arr[3]=board[y][x];
board[y][x]=0;
}
other[1]=arr[1];
other[3]=arr[3];
System.out.println(arr[1]);
}
static void swapRight(int[] arr,int[] other,int y,int x){
int a=arr[3];
for(int i=2;i>=0;i--){
arr[i+1]=arr[i];
}
arr[0]=a;
if(board[y][x]==0){
board[y][x]=arr[3];
}
else{
arr[3]=board[y][x];
board[y][x]=0;
}
other[1]=arr[1];
other[3]=arr[3];
System.out.println(arr[1]);
}
}
#구현