#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while ((c = getchar()) == ' ')
    ;
  x = c;
}
inline void writes(string s) { puts(s.c_str()); }
inline void writeln() { writes(""); }
inline void writei(int x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
inline void writell(long long x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
int n, m, i, j, a[333][333], b[333][333], cc, ord[666][666], cenx, ceny,
    cnt[180003];
long long ans;
struct node {
  int x, y, col;
};
bool cmp(node x, node y) {
  return ord[x.x - cenx + 300][x.y - ceny + 300] <
         ord[y.x - cenx + 300][y.y - ceny + 300];
}
map<int, int> mp;
vector<node> v[180003];
int f(int x) {
  if (mp.count(x)) return mp[x];
  return mp[x] = ++cc;
}
queue<node> q;
void preorder() {
  int i, j, c = 1;
  ord[300][300] = 1;
  for (((i)) = (1); ((i)) <= ((300)); ((i))++) {
    for (((j)) = (1); ((j)) <= ((i * 2)); ((j))++)
      ord[300 - i][300 - i + j] = ++c;
    for (((j)) = (1); ((j)) <= ((i * 2)); ((j))++)
      ord[300 - i + j][300 + i] = ++c;
    for (((j)) = (1); ((j)) <= ((i * 2)); ((j))++)
      ord[300 + i][300 + i - j] = ++c;
    for (((j)) = (1); ((j)) <= ((i * 2)); ((j))++)
      ord[300 + i - j][300 - i] = ++c;
  }
}
int main() {
  read(n);
  read(m);
  for (((i)) = (1); ((i)) <= ((n)); ((i))++)
    for (((j)) = (1); ((j)) <= ((m)); ((j))++) {
      read(a[i][j]);
      if (a[i][j]) a[i][j] = f(a[i][j]);
      cnt[a[i][j]]++;
    }
  for (((i)) = (1); ((i)) <= ((n)); ((i))++)
    for (((j)) = (1); ((j)) <= ((m)); ((j))++) {
      read(b[i][j]);
      if (b[i][j] && b[i][j] != -1) b[i][j] = f(b[i][j]);
      if (b[i][j] != -1) v[a[i][j]].push_back(node{i, j, b[i][j]});
    }
  preorder();
  read(cenx);
  read(ceny);
  q.push(node{cenx, ceny, b[cenx][ceny]});
  int k = a[cenx][ceny];
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, col = q.front().col;
    q.pop();
    if (!k) continue;
    if (k == col) continue;
    if (!cnt[k]) {
      k = col;
      continue;
    }
    cnt[col] += cnt[k];
    ans += cnt[k];
    cnt[k] = 0;
    cenx = x;
    ceny = y;
    sort((v[k]).begin(), (v[k]).end(), cmp);
    for (__typeof((v[k]).begin()) it = (v[k]).begin(); it != (v[k]).end(); it++)
      q.push(*it);
    v[k].clear();
    k = col;
  }
  cout << ans << endl;
  return 0;
}
