#include <bits/stdc++.h>
using namespace std;
struct pr {
  int x, y;
};
const int N = 1010;
int n, m, t, l, x, y, a[N][N], b[N][N], d[N * 2][N * 2], e[N * N * 2],
    f[N * N * 2], cr[N * N * 2];
long long ans;
vector<pr> g[N * N * 2];
queue<pr> q;
map<int, int> c;
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
bool cmp(const pr &a, const pr &b) {
  return d[a.x - x + l][a.y - y + l] < d[b.x - x + l][b.y - y + l];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < m + 1; j++) {
      scanf("%d", &a[i][j]);
      int &cl = a[i][j];
      if (cl && !c[cl]) c[cl] = ++t;
      cl = c[cl];
      e[cl]++;
    }
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < m + 1; j++) {
      scanf("%d", &b[i][j]);
      int &cl = b[i][j];
      if (cl != -1) {
        if (cl && !c[cl]) c[cl] = ++t;
        cl = c[cl], g[a[i][j]].push_back(pr{i, j});
      }
    }
  l = max(n, m);
  for (int i = 0; i < t + 1; i++) f[i] = i, cr[i] = i;
  x = y = l;
  t = 0;
  d[l][l] = 0;
  for (int i = 1; i < l; i++) {
    d[--x][y] = ++t;
    for (int j = 1; j < 2 * i; j++) d[x][++y] = ++t;
    for (int j = 0; j < 2 * i; j++) d[++x][y] = ++t;
    for (int j = 0; j < 2 * i; j++) d[x][--y] = ++t;
    for (int j = 0; j < 2 * i; j++) d[--x][y] = ++t;
  }
  scanf("%d%d", &x, &y);
  q.push(pr{x, y});
  while (q.size()) {
    x = q.front().x, y = q.front().y;
    q.pop();
    int c1 = find(a[x][y]), c2 = find(b[x][y]);
    if (!cr[c1] || cr[c1] == b[x][y]) continue;
    sort(g[c1].begin(), g[c1].end(), cmp);
    for (pr j : g[c1]) q.push(j);
    g[c1].clear();
    ans += e[c1];
    if (c1 != c2) e[c2] += e[c1], f[c1] = c2;
    cr[c2] = b[x][y];
  }
  printf("%I64d\n", ans);
}
