#include <bits/stdc++.h>
using namespace std;
int order[602][602];
int m, n;
int panel[302][302], symbol[302][302];
vector<pair<int, int> > bucket[2 * 302 * 302];
int weight[2 * 302 * 302], pre[2 * 302 * 302];
map<int, int> mp;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int posx, posy, step = 1, direc = 0, total = 0;
int get(int A) {
  if (!mp.count(A)) mp[A] = total++;
  return mp[A];
}
int root(int x) {
  int y = x;
  while (pre[y] != y) y = pre[y];
  while (x != y) {
    int z = pre[x];
    pre[x] = y;
    x = z;
  }
  return x;
}
void link(int u, int v) {
  u = root(u);
  v = root(v);
  pre[u] = v;
  weight[v] += weight[u];
  weight[u] = 0;
}
bool cmp(pair<int, int> A, pair<int, int> B) {
  int dx = 300 - posx, dy = 300 - posy;
  return order[A.first + dx][A.second + dy] <
         order[B.first + dx][B.second + dy];
}
int main() {
  memset(order, -1, sizeof(order));
  order[300][300] = 0;
  posx = 300;
  posy = 300;
  while (posx >= 0 && posx <= 600 && posy >= 0 && posy <= 600) {
    for (int i = 0; i < step; i++) {
      posx += dx[direc];
      posy += dy[direc];
      if (posx >= 0 && posx <= 600 && posy >= 0 && posy <= 600)
        order[posx][posy] = ++total;
    }
    direc = (direc + 1) % 4;
    for (int i = 0; i < step; i++) {
      posx += dx[direc];
      posy += dy[direc];
      if (posx >= 0 && posx <= 600 && posy >= 0 && posy <= 600)
        order[posx][posy] = ++total;
    }
    step++;
    direc = (direc + 1) % 4;
  }
  scanf("%d %d", &m, &n);
  total = 1;
  mp[0] = 0;
  if (1) {
    total = 4;
    for (int i = 0; i < total; i++) mp[i] = i;
  }
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
      scanf("%d", &panel[i][j]);
      panel[i][j] = get(panel[i][j]);
      weight[panel[i][j]]++;
    }
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
      scanf("%d", &symbol[i][j]);
      if (symbol[i][j] >= 0) symbol[i][j] = get(symbol[i][j]);
    }
  for (int i = 0; i < total; i++) pre[i] = i;
  scanf("%d %d", &posx, &posy);
  posx--;
  posy--;
  queue<pair<int, int> > q;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      if (i == posx && j == posy)
        q.push(make_pair(i, j));
      else if (symbol[i][j] >= 0)
        bucket[panel[i][j]].push_back(make_pair(i, j));
  long long ret = 0;
  while (!q.empty()) {
    posx = q.front().first;
    posy = q.front().second;
    q.pop();
    int first = symbol[posx][posy], last = root(panel[posx][posy]);
    if (last == 0 || last == first) continue;
    ret += weight[last];
    vector<pair<int, int> > v = bucket[last];
    sort(v.begin(), v.end(), cmp);
    for (int i = 0; i < v.size(); i++) q.push(v[i]);
    bucket[last].clear();
    pre[first] = first;
    link(last, first);
  }
  cout << ret << endl;
}
