#include <bits/stdc++.h>
using namespace std;
int panels[320][320], symbols[320][320];
int rep[320][320];
int colorRep[320 * 320];
int lenRep[320 * 320];
int n, m;
queue<pair<int, int> > fila;
set<pair<int, pair<int, int> > > symbolsFromColor;
map<int, int> mapa;
int color[200100];
void join(int a, int b) {
  if (a == b) return;
  if (rand() % 2) {
    rep[a / m][a % m] = b;
    lenRep[b] += lenRep[a];
  } else {
    rep[b / m][b % m] = a;
    lenRep[a] += lenRep[b];
  }
}
int find(int node) {
  if (rep[node / m][node % m] != node) {
    rep[node / m][node % m] = find(rep[node / m][node % m]);
  }
  return rep[node / m][node % m];
}
int getColorPanel(int x, int y) {
  int s = find(x * m + y);
  return colorRep[s];
}
int getVal(int x, int y) {
  int len = max(abs(x), abs(y));
  int first = (2 * len - 1) * (2 * len - 1);
  if (x == -len) {
    first += 2 * len - 1;
    first += 2 * len;
    first += 2 * len;
    first += (y - (-len));
  } else if (y == -len) {
    first += 2 * len - 1;
    first += 2 * len;
    first += (len - x);
  } else if (x == len) {
    first += 2 * len - 1;
    first += (len - y);
  } else {
    first += (x - (-len + 1));
  }
  return first;
}
void process(int x, int y, int cor) {
  vector<pair<int, pair<int, int> > > novos;
  set<pair<int, pair<int, int> > >::iterator it, it2;
  it = symbolsFromColor.lower_bound(make_pair(cor, make_pair(-10, -10)));
  while (it != symbolsFromColor.end() && it->first == cor) {
    it2 = it;
    it2++;
    novos.push_back(pair<int, pair<int, int> >(0, it->second));
    symbolsFromColor.erase(it);
    it = it2;
  }
  for (int i = 0; i < novos.size(); i++) {
    novos[i].first =
        getVal(novos[i].second.second - y, x - novos[i].second.first);
  }
  sort(novos.begin(), novos.end());
  for (int i = 0; i < novos.size(); i++) {
    fila.push(novos[i].second);
  }
}
int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &panels[i][j]);
      mapa[panels[i][j]] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &symbols[i][j]);
      if (symbols[i][j] != -1) {
        mapa[symbols[i][j]] = 1;
      }
    }
  }
  mapa[0] = 0;
  int cc = 0;
  for (map<int, int>::iterator it = mapa.begin(); it != mapa.end(); it++) {
    it->second = cc;
    cc++;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      panels[i][j] = mapa[panels[i][j]];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (symbols[i][j] != -1) {
        symbols[i][j] = mapa[symbols[i][j]];
      }
    }
  }
  for (int i = 0; i < cc; i++) {
    color[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      rep[i][j] = m * i + j;
      color[panels[i][j]] = rep[i][j];
      colorRep[i * m + j] = panels[i][j];
      lenRep[i * m + j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      rep[i][j] = color[panels[i][j]];
      lenRep[rep[i][j]]++;
    }
  }
  scanf("%d%d", &x, &y);
  x--;
  y--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (symbols[i][j] != -1 && !(x == i && y == j)) {
        symbolsFromColor.insert(make_pair(panels[i][j], pair<int, int>(i, j)));
      }
    }
  }
  long long res = 0;
  fila.push(pair<int, int>(x, y));
  while (!fila.empty()) {
    int ax = fila.front().first;
    int ay = fila.front().second;
    fila.pop();
    int cor = getColorPanel(ax, ay);
    if (cor != 0 && symbols[ax][ay] != cor) {
      process(ax, ay, cor);
      res += lenRep[find(ax * m + ay)];
      if (color[symbols[ax][ay]] == -1) {
        color[symbols[ax][ay]] = find(ax * m + ay);
        colorRep[find(ax * m + ay)] = symbols[ax][ay];
      } else {
        join(color[symbols[ax][ay]], find(ax * m + ay));
        color[symbols[ax][ay]] = find(ax * m + ay);
        colorRep[find(ax * m + ay)] = symbols[ax][ay];
      }
      color[cor] = -1;
    }
  }
  cout << res << endl;
  return 0;
}
