#include <bits/stdc++.h>
using namespace std;
int s, t[10][10];
int l1[10], r1[10];
int l2[10], r2[10];
int n, m, k;
int len;
int a[100001];
int main() {
  scanf("%d%d%d%d", &n, &m, &k, &len);
  for (int i = 1; i <= k; i++) l1[i] = l2[i] = n + m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &s);
      l1[s] = min(l1[s], i + j - 1);
      r1[s] = max(r1[s], i + j - 1);
      l2[s] = min(l2[s], j + n - i);
      r2[s] = max(r2[s], j + n - i);
    }
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      t[i][j] = max(max(r1[j] - l1[i], l1[j] - r1[i]),
                    max(r2[j] - l2[i], l2[j] - r2[i]));
  for (int i = 1; i <= len; i++) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 1; i < len; i++) {
    ans = max(ans, t[a[i]][a[i + 1]]);
    ans = max(ans, t[a[i + 1]][a[i]]);
  }
  printf("%d\n", ans);
  return 0;
}
