#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 1, -1, -1};
int dy[4] = {1, -1, 1, -1};
int ma[10][4], id[10][4], r[10][10];
const int INF = (int)1e9;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int k, s;
  scanf("%d%d", &k, &s);
  for (int i = 0; i < (k); ++i)
    for (int j = 0; j < (4); ++j) ma[i][j] = -INF;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      int(x);
      scanf("%d", &x);
      x--;
      for (int k = 0; k < (4); ++k) {
        int v = i * dx[k] + j * dy[k];
        if (v > ma[x][k]) {
          ma[x][k] = v;
          id[x][k] = i * m + j;
        }
      }
    }
  for (int i = 0; i < (k); ++i)
    for (int j = 0; j < (k); ++j) {
      for (int i2 = 0; i2 < (4); ++i2)
        for (int j2 = 0; j2 < (4); ++j2) {
          r[i][j] = max(r[i][j], abs(id[i][i2] / m - id[j][j2] / m) +
                                     abs(id[i][i2] % m - id[j][j2] % m));
        }
    }
  int an = 0;
  int last = -1;
  for (int i = 0; i < (s); ++i) {
    int(x);
    scanf("%d", &x);
    x--;
    if (last != -1) {
      an = max(an, r[last][x]);
    }
    last = x;
  }
  printf("%d\n", an);
  return 0;
}
