#include <bits/stdc++.h>
using namespace std;
int mn1[12], mx1[12];
int mn2[12], mx2[12];
int calc(int x, int y) {
  if (mx1[x] == 0 || mx1[y] == 0) return 0;
  return max(max(abs(mx1[x] - mn1[y]), abs(mx1[y] - mn1[x])),
             max(abs(mx2[x] - mn2[y]), abs(mx2[y] - mn2[x])));
}
int main() {
  int n, m, k, s;
  scanf("%d %d %d %d", &n, &m, &k, &s);
  for (int i = 0; i < 12; i++) mn1[i] = mn2[i] = 1e8;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      scanf("%d", &x);
      mn1[x] = min(mn1[x], i + m - j + 1);
      mx1[x] = max(mx1[x], i + m - j + 1);
      mn2[x] = min(mn2[x], i + j);
      mx2[x] = max(mx2[x], i + j);
    }
  }
  int ans = 0;
  int x, prvx = -1;
  while (s--) {
    scanf("%d", &x);
    if (prvx != -1) ans = max(ans, calc(x, prvx));
    prvx = x;
  }
  printf("%d\n", ans);
  return 0;
}
