#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
int a[2010][2010];
int Max[4][10];
int ans[10][10];
int main() {
  int n, m, k, s, x, y;
  scanf("%d%d%d%d", &n, &m, &k, &s);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      x = a[i][j];
      Max[0][x] = max(i + j, Max[0][x]);
      Max[1][x] = max(i + n - j, Max[1][x]);
      Max[2][x] = max(n - i + j, Max[2][x]);
      Max[3][x] = max(n - i + n - j, Max[3][x]);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      x = a[i][j];
      for (int k = 1; k <= 9; k++) {
        ans[x][k] = max(Max[0][k] - (i + j), ans[x][k]);
        ans[x][k] = max(Max[1][k] - (i + n - j), ans[x][k]);
        ans[x][k] = max(Max[2][k] - (n - i + j), ans[x][k]);
        ans[x][k] = max(Max[3][k] - (n - i + n - j), ans[x][k]);
      }
    }
  }
  int t = 0;
  scanf("%d", &x);
  for (int i = 1; i < s; i++) {
    scanf("%d", &y);
    t = max(ans[x][y], t);
    x = y;
  }
  printf("%d\n", t);
  return 0;
}
