#include <bits/stdc++.h>
using namespace std;
const bool debug = false;
const int maxn = 303;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, spiral[2 * maxn + 1][2 * maxn + 1], color[maxn][maxn],
    panel[maxn][maxn];
map<int, vector<int> > lst;
void generateSpiral() {
  int x = maxn, y = maxn, cnt = 0, step = 1, dir = 0;
  spiral[maxn][maxn] = cnt++;
  for (int i = 0;; i++, dir = (dir + 1) % 4) {
    for (int j = 0; j < step; j++) {
      if (x + dx[dir] < 0 || x + dx[dir] >= 2 * maxn + 1 || y + dy[dir] < 0 ||
          y + dy[dir] >= 2 * maxn + 1)
        return;
      spiral[x += dx[dir]][y += dy[dir]] = cnt++;
    }
    if (i & 1) step++;
  }
}
int main() {
  generateSpiral();
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> color[i][j];
      lst[color[i][j]].push_back(i * m + j);
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> panel[i][j];
  int x, y;
  cin >> x >> y, x--, y--;
  queue<int> q;
  q.push(x * m + y);
  long long answer = 0, current = 0;
  int prev = color[x][y];
  while (q.size()) {
    int v = q.front();
    q.pop();
    x = v / m, y = v % m;
    if (prev == 0 || prev == panel[x][y] || panel[x][y] == -1) continue;
    vector<pair<int, int> > toAdd;
    for (size_t i = 0; i < lst[prev].size(); i++) {
      int x1 = lst[prev][i] / m, y1 = lst[prev][i] % m;
      toAdd.push_back(
          make_pair(spiral[maxn + x1 - x][maxn + y1 - y], lst[prev][i]));
    }
    sort(toAdd.begin(), toAdd.end());
    current += toAdd.size();
    answer += current;
    for (size_t i = 0; i < toAdd.size(); i++)
      if (toAdd[i].second != v) q.push(toAdd[i].second);
    lst[prev].clear();
    prev = panel[x][y];
  }
  cout << answer;
  return 0;
}
