#include <iostream>
using namespace std;
const int MAX_N = 51;
const int MAX_M = 51;
int arr[MAX_N][MAX_M];
int clean;
int N, M;
int robotX, robotY;
int dir;
void turnLeft() {
dir = (dir + 3) % 4;
}
const int DIR_UP = 0;
const int DIR_RIGHT = 1;
const int DIR_DOWN = 2;
const int DIR_LEFT = 3;
void move() {
if (dir == DIR_UP && arr[robotX - 1][robotY] == 0) {
robotX--;
} else if (dir == DIR_RIGHT && arr[robotX][robotY + 1] == 0) {
robotY++;
} else if (dir == DIR_DOWN && arr[robotX + 1][robotY] == 0) {
robotX++;
} else if (dir == DIR_LEFT && arr[robotX][robotY - 1] == 0) {
robotY--;
}
}
void moveBackward() {
if (dir == DIR_UP) {
if (robotX + 1 < N && arr[robotX + 1][robotY] != 1) {
robotX++;
return;
}
} else if (dir == DIR_RIGHT) {
if (robotY - 1 >= 0 && arr[robotX][robotY - 1] != 1) {
robotY--;
return;
}
} else if (dir == DIR_DOWN) {
if (robotX - 1 >= 0 && arr[robotX - 1][robotY] != 1) {
robotX--;
return;
}
} else if (dir == DIR_LEFT) {
if (robotY + 1 < M && arr[robotX][robotY + 1] != 1) {
robotY++;
return;
}
}
cout << clean << "\n";
exit(0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
cin >> robotX >> robotY >> dir;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
}
}
while (true) {
if (arr[robotX][robotY] == 0) {
clean++;
arr[robotX][robotY] = 2;
}
bool canMove = false;
for (int i = 0; i < 4; i++) {
turnLeft();
move();
if (arr[robotX][robotY] == 0) {
canMove = true;
break;
}
}
if (!canMove) {
moveBackward();
}
}
return 0;
}