#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int r, c, k, n, max_col[10][2002], min_col[10][2002];
int arr[2002][2002], seq[100005];
bool present[10][10];
int main() {
  scanf("%d%d%d%d", &r, &c, &k, &n);
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) scanf("%d", &arr[i][j]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &seq[i]);
    present[seq[i]][seq[i - 1]] = true;
  }
  for (int z = 1; z <= k; z++)
    for (int i = 1; i <= r; i++) {
      min_col[z][i] = (1000000007LL);
      max_col[z][i] = -(1000000007LL);
    }
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      min_col[arr[i][j]][i] = min(min_col[arr[i][j]][i], j);
      max_col[arr[i][j]][i] = max(max_col[arr[i][j]][i], j);
    }
  int ans = 0;
  for (int z1 = 1; z1 <= k; z1++)
    for (int z2 = 1; z2 <= k; z2++)
      for (int i1 = 1; i1 <= r; i1++)
        for (int i2 = 1; i2 <= r; i2++) {
          if (!present[z1][z2]) continue;
          if (min_col[z1][i1] != (1000000007LL) &&
              max_col[z2][i2] != -(1000000007LL) &&
              max_col[z2][i2] >= min_col[z1][i1])
            ans = max(ans, abs(i1 - i2) + max_col[z2][i2] - min_col[z1][i1]);
          if (min_col[z2][i2] != (1000000007LL) &&
              max_col[z1][i1] != -(1000000007LL) &&
              max_col[z1][i1] >= min_col[z2][i2])
            ans = max(ans, abs(i1 - i2) + max_col[z1][i1] - min_col[z2][i2]);
        }
  printf("%d\n", ans);
  return 0;
}
