#include <iostream>
using namespace std;
int area[50][50];
int dir[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
int Cnt = 1;
int turn_left(int d){
return (4 + d - 1) % 4;
}
int turn_back(int d) {
return (4 + d - 2) % 4;
}
void solution(int r, int c, int d, int check) {
area[r][c] = 2;
int left_d = turn_left(d);
int back_d = turn_back(d);
if (area[r + dir[left_d][0]][c + dir[left_d][1]] == 0) {
Cnt++;
solution(r + dir[left_d][0], c + dir[left_d][1], left_d, 0);
}
else if (area[r + dir[left_d][0]][c + dir[left_d][1]] != 0) {
if (check != 4) {
solution(r, c, left_d, check+1);
}
else {
if (area[r + dir[back_d][0]][c + dir[back_d][1]] == 1)
return;
else solution(r + dir[back_d][0], c + dir[back_d][1], d, 0);
}
}
}
int main(){
int N, M;
int r, c, d;
cin >> N >> M;
cin >> r >> c >> d;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> area[i][j];
}
}
solution(r, c, d, 0);
cout << Cnt << endl;
return 0;
}