#include <bits/stdc++.h>
using namespace std;
int n, m;
int k;
bool debug = false;
int mp[305][305], tower[305][305];
bool bl[305][305];
map<int, vector<pair<int, int>>> ta;
map<int, int> sz;
queue<pair<int, int>> q;
long long ans;
int dis(int i, int j, int x, int y) {
  int dd = max(abs(i - x), abs(j - y));
  if (dd == 0) return 1;
  int u = i - dd, d = i + dd, l = j - dd, r = j + dd;
  int edge = 2 * dd + 1;
  if (u == x && l == y) return edge * edge;
  int lastM = (edge - 2) * (edge - 2);
  if (x == u) {
    return lastM + y - l;
  }
  if (y == r) {
    return lastM + (edge - 1) + x - u;
  }
  if (x == d) {
    return lastM + edge - 2 + edge + (r - y);
  }
  if (y == l) {
    return lastM + edge + edge - 2 + edge - 1 + d - x;
  }
  return -1 << 30;
}
int sx, sy;
bool cmp(pair<int, int> a, pair<int, int> b) {
  int da = dis(sx, sy, a.first, a.second);
  int db = dis(sx, sy, b.first, b.second);
  return da < db;
}
void setQue(int color, pair<int, int> p) {
  ans += sz[color];
  int toColor = tower[p.first][p.second];
  sz[toColor] += sz[color];
  sz[color] = 0;
  vector<pair<int, int>> v = ta[color];
  sx = p.first;
  sy = p.second;
  sort(v.begin(), v.end(), cmp);
  for (auto x : v)
    if (!bl[x.first][x.second]) {
      q.push(x);
      bl[x.first][x.second] = 1;
    }
  ta[color].clear();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &mp[i][j]);
      sz[mp[i][j]]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &k);
      tower[i][j] = k;
      if (k != -1) {
        ta[mp[i][j]].push_back({i, j});
      }
    }
  }
  int x, y;
  scanf("%d%d", &x, &y);
  q.push({x, y});
  bl[x][y] = 1;
  int color = mp[x][y];
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    if (color == 0 || color == tower[t.first][t.second]) continue;
    setQue(color, t);
    color = tower[t.first][t.second];
  }
  cout << ans << endl;
  return 0;
}
