#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
int n, m, k, s;
int Max[10][4], Min[10][4];
int q[100010];
template <typename Type>
inline void cmax(Type& a, Type b) {
  a = max(a, b);
}
template <typename Type>
inline void cmin(Type& a, Type b) {
  a = min(a, b);
}
int main() {
  while (~scanf("%d%d%d%d", &n, &m, &k, &s)) {
    for (int i = 0; i < 10; ++i)
      for (int j = 0; j < 4; ++j) Max[i][j] = -INF, Min[i][j] = INF;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        int x;
        scanf("%d", &x);
        cmax(Max[x][0], -i - j);
        cmax(Max[x][1], -i + j);
        cmax(Max[x][2], i - j);
        cmax(Max[x][3], i + j);
        cmin(Min[x][0], -i - j);
        cmin(Min[x][1], -i + j);
        cmin(Min[x][2], i - j);
        cmin(Min[x][3], i + j);
      }
    int ans = 0;
    for (int i = 0; i < s; ++i) {
      scanf("%d", &q[i]);
      if (!i) continue;
      int tmp = 0;
      for (int k = 0; k < 4; ++k) {
        if (q[i] == q[i - 1]) {
          cmax(tmp, Max[q[i]][k] - Min[q[i]][k]);
        } else {
          cmax(tmp, Max[q[i - 1]][k] - Min[q[i]][k]);
          cmax(tmp, Max[q[i]][k] - Min[q[i - 1]][k]);
        }
      }
      cmax(ans, tmp);
    }
    printf("%d\n", ans);
  }
  return 0;
}
