#include <bits/stdc++.h>
using namespace std;
const int M = 2010;
int val[20][20];
int dist[20][20];
int main() {
  ios_base::sync_with_stdio(false);
  int i, j, k, n, m, foo, bar;
  for (i = 0; i < 11; i++)
    val[i][0] = val[i][1] = -1111111111, val[i][2] = val[i][3] = 1111111111;
  scanf("%d%d", &n, &m);
  scanf("%d%d", &foo, &bar);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &k);
      val[k][0] = max(val[k][0], i + j);
      val[k][1] = max(val[k][1], i - j);
      val[k][2] = min(val[k][2], i + j);
      val[k][3] = min(val[k][3], i - j);
    }
  }
  for (i = 1; i <= foo; i++) {
    for (j = 1; j <= foo; j++) {
      int a = val[i][0] - val[j][2];
      int b = val[i][1] - val[j][3];
      int c = val[j][1] - val[i][3];
      int d = val[j][0] - val[i][2];
      dist[i][j] = max(max(a, b), max(c, d));
    }
  }
  int maxx = -1111111111;
  scanf("%d", &i);
  for (k = 1; k < bar; k++) {
    scanf("%d", &j);
    maxx = max(maxx, dist[i][j]);
    i = j;
  }
  printf("%d\n", maxx);
  return 0;
}
