#include <bits/stdc++.h>
using namespace std;
const int maxn = 305, maxm = 180005;
int a[maxn][maxn], b[maxn][maxn], s[maxm];
vector<pair<int, int> > node[maxm];
queue<pair<int, int> > q;
map<int, int> col;
int n, m, tot;
long long ans;
pair<int, int> now;
int getcol(int x) {
  if (!col.count(x)) col[x] = tot++;
  return col[x];
}
inline int dis(int x, int y) {
  int n;
  if (y <= 0)
    n = max(abs(y), abs(x) - 1);
  else
    n = max(abs(y), abs(x)) - 1;
  if (x == n + 1) return 4 * (n + 1) * (n + 1) + n + 1 - y;
  if (y == n + 1) return (4 * n + 2) * (n + 1) + n + 1 + x;
  if (x == -n - 1) return (2 * n + 1) * (2 * n + 1) + n + y;
  return 4 * n * n + 2 * n + n - x;
}
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return dis(a.first - now.first, a.second - now.second) <
         dis(b.first - now.first, b.second - now.second);
}
int main() {
  int i, j, x, y, k, last, cnow, snow;
  scanf("%d%d", &n, &m);
  col[0] = tot++;
  for (i = 0; i < n; ++i)
    for (j = 0; j < m; ++j) {
      scanf("%d", a[i] + j);
      a[i][j] = getcol(a[i][j]);
      ++s[a[i][j]];
    }
  for (i = 0; i < n; ++i)
    for (j = 0; j < m; ++j) {
      scanf("%d", b[i] + j);
      if (b[i][j] != -1) b[i][j] = getcol(b[i][j]);
    }
  scanf("%d%d", &x, &y);
  --x;
  --y;
  q.push(pair<int, int>(x, y));
  for (i = 0; i < n; ++i)
    for (j = 0; j < m; ++j)
      if ((i != x || j != y) && b[i][j] != -1)
        node[a[i][j]].push_back(pair<int, int>(i, j));
  last = a[x][y];
  while (!q.empty()) {
    now = q.front();
    q.pop();
    i = now.first;
    j = now.second;
    cnow = b[i][j];
    if (last == 0 || last == cnow) continue;
    snow += s[last];
    s[last] = 0;
    ans += snow;
    vector<pair<int, int> > elm = node[last];
    node[last].clear();
    sort(elm.begin(), elm.end(), cmp);
    for (k = 0; k < (int)elm.size(); ++k) q.push(elm[k]);
    last = cnow;
  }
  printf("%I64d\n", ans);
  return 0;
}
