#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[310][310], b[310][310];
map<int, vector<pair<int, int> > > s;
map<int, int> cnt;
map<pair<int, int>, pair<int, int> > f;
map<int, pair<int, int> > leader;
map<pair<int, int>, int> col;
pair<int, int> getfa(pair<int, int> x) {
  if (f[x] == x) return f[x] = x;
  return f[x] = getfa(f[x]);
}
pair<int, int> ori;
map<pair<int, int>, int> num;
void init() {
  int cnt = 0;
  num[make_pair(0, 0)] = ++cnt;
  for (int d = (1); d <= (max(n, m) + 10); d++) {
    for (int i = (-d + 1); i <= (d); i++) num[make_pair(-d, i)] = ++cnt;
    for (int i = (-d + 1); i <= (d); i++) num[make_pair(i, d)] = ++cnt;
    for (int i = (d - 1); i >= (-d); i--) num[make_pair(d, i)] = ++cnt;
    for (int i = (d - 1); i >= (-d); i--) num[make_pair(i, -d)] = ++cnt;
  }
}
void output(pair<int, int> a, string s = "") {
  cout << "(" << a.first << "," << a.second << ")" << s;
}
void fuck() {
  for (int i = (1); i <= (n); i++) {
    for (int j = (1); j <= (m); j++) output(getfa(make_pair(i, j)), " ");
    cout << endl;
  }
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  a.first -= ori.first, a.second -= ori.second;
  b.first -= ori.first, b.second -= ori.second;
  return num[a] < num[b];
}
int main() {
  cin >> n >> m;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      scanf("%d", a[i] + j);
      cnt[a[i][j]]++;
      if (leader[a[i][j]] != make_pair(0, 0)) {
        f[make_pair(i, j)] = leader[a[i][j]];
      } else {
        f[make_pair(i, j)] = make_pair(i, j);
        leader[a[i][j]] = make_pair(i, j);
        col[make_pair(i, j)] = a[i][j];
      }
    }
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      scanf("%d", b[i] + j);
      if (b[i][j] != -1) s[a[i][j]].push_back(make_pair(i, j));
    }
  init();
  int x, y;
  cin >> x >> y;
  queue<pair<int, int> > Q;
  Q.push(make_pair(x, y));
  s[a[x][y]].erase(
      find((s[a[x][y]]).begin(), (s[a[x][y]]).end(), make_pair(x, y)));
  long long ans = 0;
  while (!Q.empty()) {
    pair<int, int> now = Q.front();
    Q.pop();
    int p = col[getfa(now)], q = b[now.first][now.second];
    if (p && q != p) {
      cnt[q] += cnt[p];
      ans += cnt[p];
      cnt[p] = 0;
      if (leader[q] == make_pair(0, 0)) {
        leader[q] = getfa(now);
        col[getfa(now)] = q;
      } else {
        f[getfa(now)] = leader[q];
      }
      leader[p] = make_pair(0, 0);
      vector<pair<int, int> > &c = s[p];
      ori = now;
      sort((c).begin(), (c).end(), cmp);
      for (typeof((c).begin()) e = (c).begin(); e != (c).end(); e++) Q.push(*e);
      c.clear();
    }
  }
  cout << ans << endl;
  return 0;
}
