#include <bits/stdc++.h>
using namespace std;
int mat[2222][2222], a[2222];
int mx[11][2], mn[11][2];
int dis[11][11];
int main() {
  int n, m, k, s;
  scanf("%d %d %d %d", &n, &m, &k, &s);
  for (int i = 1; i <= k; i++) {
    mn[i][0] = mn[i][1] = 2222 + 2222;
    mx[i][0] = mx[i][1] = -2222 - 2222;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", mat[i] + j);
      int u = mat[i][j];
      mx[u][0] = max(mx[u][0], i + j);
      mn[u][0] = min(mn[u][0], i + j);
      mx[u][1] = max(mx[u][1], i - j);
      mn[u][1] = min(mn[u][1], i - j);
    }
  }
  for (int i = 0; i < s; i++) scanf("%d", a + i);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int u = mat[i][j];
      for (int v = 1; v <= k; v++) {
        int d = 0;
        d = max(d, abs(i + j - mn[v][0]));
        d = max(d, abs(i + j - mx[v][0]));
        d = max(d, abs(i - j - mn[v][1]));
        d = max(d, abs(i - j - mx[v][1]));
        dis[v][u] = dis[u][v] = max(dis[u][v], d);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i < s; i++) {
    ans = max(ans, dis[a[i - 1]][a[i]]);
  }
  printf("%d\n", ans);
  return 0;
}
