#include <bits/stdc++.h>
using namespace std;
enum { MAXQ = 180010, MAXN = 310 };
struct node {
  int x, y, z;
  bool operator<(const node &t) const { return z < t.z; }
} tmp, q[MAXQ];
int a[MAXN * 2][MAXN * 2];
int m, n, c[MAXN][MAXN], s[MAXN][MAXN], o[MAXQ], w, ww, x, y, F[MAXQ], S[MAXQ],
    rq, wq;
long long out;
vector<node> h[MAXQ];
void init() {
  memset(a, 0, sizeof(a));
  int x = 300, y = 300, z = 1, dir = 0;
  a[x][y] = z;
  while (z < 360000) {
    if (dir == 0) {
      a[--x][y] = ++z;
      if (a[x][y + 1] == 0) dir = (dir + 1) % 4;
    } else if (dir == 1) {
      a[x][++y] = ++z;
      if (a[x + 1][y] == 0) dir = (dir + 1) % 4;
    } else if (dir == 2) {
      a[++x][y] = ++z;
      if (a[x][y - 1] == 0) dir = (dir + 1) % 4;
    } else if (dir == 3) {
      a[x][--y] = ++z;
      if (a[x - 1][y] == 0) dir = (dir + 1) % 4;
    }
  }
}
int ref(int z) {
  int mi = 0, mid, ma = ww - 1;
  while (mi <= ma) {
    mid = (mi + ma) >> 1;
    if (z == o[mid]) return mid;
    if (z < o[mid])
      ma = mid - 1;
    else
      mi = mid + 1;
  }
}
int Q[MAXQ], wQ;
int find(int z) {
  wQ = 0;
  Q[wQ] = z;
  while (F[Q[wQ]] != Q[wQ]) {
    Q[wQ + 1] = F[Q[wQ]];
    wQ++;
  }
  for (int i = 0; i < wQ; i++) {
    F[Q[i]] = Q[wQ];
  }
  return Q[wQ];
}
int fun(int x, int y, int X, int Y) {
  int dx = 300 + X - x;
  int dy = 300 + Y - y;
  return a[dx][dy];
}
int main() {
  init();
  scanf("%d%d", &m, &n);
  w = 0;
  o[w++] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &c[i][j]);
      o[w++] = c[i][j];
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &s[i][j]);
      if (s[i][j] != -1) o[w++] = s[i][j];
    }
  }
  sort(o, o + w);
  ww = 1;
  for (int i = 1; i < w; i++) {
    if (o[i] != o[i - 1]) {
      o[ww++] = o[i];
    }
  }
  for (int i = 0; i < ww; i++) {
    F[i] = i;
    S[i] = 0;
  }
  scanf("%d%d", &x, &y);
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      c[i][j] = ref(c[i][j]);
      S[c[i][j]]++;
      if (s[i][j] != -1) {
        s[i][j] = ref(s[i][j]);
        if ((i != x) || (j != y)) {
          tmp.x = i;
          tmp.y = j;
          h[c[i][j]].push_back(tmp);
        }
      }
    }
  }
  out = 0;
  rq = wq = 0;
  tmp.x = x;
  tmp.y = y;
  q[wq++] = tmp;
  while (rq != wq) {
    tmp = q[rq];
    x = tmp.x;
    y = tmp.y;
    c[x][y] = find(c[x][y]);
    if ((c[x][y] != 0) && (c[x][y] != s[x][y])) {
      int size = h[c[x][y]].size();
      for (int i = 0; i < size; i++) {
        tmp = h[c[x][y]][i];
        h[c[x][y]][i].z = fun(x, y, tmp.x, tmp.y);
      }
      for (int i = 0; i < size; i++) {
        q[wq++] = h[c[x][y]][i];
      }
      h[c[x][y]].clear();
      sort(q + wq - size, q + wq);
      out += S[c[x][y]];
      int tmp = find(s[x][y]);
      if (tmp != s[x][y]) {
        S[s[x][y]] = S[tmp];
        F[tmp] = s[x][y];
        F[s[x][y]] = s[x][y];
      }
      c[x][y] = find(c[x][y]);
      if (c[x][y] != s[x][y]) {
        F[c[x][y]] = s[x][y];
        S[s[x][y]] += S[c[x][y]];
      }
    }
    rq++;
  }
  cout << out << endl;
  return 0;
}
