#include <bits/stdc++.h>
using namespace std;
long long rdtsc() {
  long long tmp;
  asm("rdtsc" : "=A"(tmp));
  return tmp;
}
inline int myrand() { return abs((rand() << 15) ^ rand()); }
inline int rnd(int x) { return myrand() % x; }
const int INF = (int)1e9 + 1;
const long double EPS = 1e-9;
void precalc() {}
int n, m, k, s;
const int maxn = (int)2e3 + 10;
int a[maxn][maxn];
const int maxs = (int)1e5 + 10, maxk = 9 + 2;
int q[maxs];
bool read() {
  if (scanf("%d%d%d%d", &n, &m, &k, &s) < 4) {
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &a[i][j]);
      --a[i][j];
    }
  }
  for (int i = 0; i < s; ++i) {
    scanf("%d", &q[i]);
    --q[i];
  }
  return 1;
}
int cool[maxk][4 * maxn][2];
int cnt[maxk];
int vis[maxk][maxk];
int getValue(int col, int ncol) {
  int res = -INF;
  int values[4];
  for (int i = 0; i < 4; ++i) {
    values[i] = -INF;
  }
  for (int who = 0; who < cnt[col]; ++who) {
    int x = cool[col][who][0], y = cool[col][who][1];
    values[0] = max(values[0], x - y);
    values[1] = max(values[1], x + y);
    values[2] = max(values[2], -x - y);
    values[3] = max(values[3], -x + y);
  }
  for (int who = 0; who < cnt[ncol]; ++who) {
    int x = cool[ncol][who][0], y = cool[ncol][who][1];
    int value = -INF;
    value = max(value, -x + y + values[0]);
    value = max(value, -x - y + values[1]);
    value = max(value, x + y + values[2]);
    value = max(value, x - y + values[3]);
    res = max(res, value);
  }
  vis[col][ncol] = vis[ncol][col] = 1;
  return res;
}
void solve() {
  for (int c = 0; c < k; ++c) {
    cnt[c] = 0;
    for (int i = 0; i < n; ++i) {
      int last = -1;
      for (int j = 0; j < m; ++j) {
        if (a[i][j] != c) {
          continue;
        }
        if (last == -1) {
          cool[c][cnt[c]][0] = i, cool[c][cnt[c]][1] = j, ++cnt[c];
        }
        last = j;
      }
      if (last != -1 && cool[c][cnt[c]][1] != last) {
        cool[c][cnt[c]][0] = i, cool[c][cnt[c]][1] = last, ++cnt[c];
      }
    }
    for (int i = 0; i < m; ++i) {
      int last = -1;
      for (int j = 0; j < n; ++j) {
        if (a[j][i] != c) {
          continue;
        }
        if (last == -1) {
          cool[c][cnt[c]][0] = j, cool[c][cnt[c]][1] = i, ++cnt[c];
        }
        last = j;
      }
      if (last != -1 && cool[c][cnt[c]][0] != last) {
        cool[c][cnt[c]][0] = last, cool[c][cnt[c]][1] = i, ++cnt[c];
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < k; ++j) {
      vis[i][j] = 0;
    }
  }
  int res = -INF;
  for (int i = 0; i < s - 1; ++i) {
    if (vis[q[i]][q[i + 1]]) {
      continue;
    }
    res = max(res, getValue(q[i], q[i + 1]));
  }
  printf("%d\n", res);
}
int main() {
  srand(rdtsc());
  precalc();
  while (1) {
    if (!read()) {
      break;
    }
    solve();
  }
  return 0;
}
