#include <bits/stdc++.h>
using namespace std;
int n, m, k, s;
int a[2002][2002];
int b[1000001];
int pos[2][2002][10];
int kol[10][10];
int main() {
  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]);
      --a[i][j];
    }
  for (int i = 0; i < s; ++i) {
    scanf("%d", &b[i]);
    --b[i];
    if (i) kol[b[i - 1]][b[i]] = 1;
  }
  memset(pos, -1, sizeof(pos));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j)
      if (pos[0][i][a[i][j]] == -1) pos[0][i][a[i][j]] = j;
    for (int j = m - 1; j >= 0; --j)
      if (pos[1][i][a[i][j]] == -1) pos[1][i][a[i][j]] = j;
  }
  int ans = 0;
  for (int i1 = 0; i1 < 10; ++i1)
    for (int i2 = 0; i2 < 10; ++i2) {
      if (!kol[i1][i2]) continue;
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          for (int t = 0; t < 2; ++t)
            if (pos[t][i][i1] != -1 && pos[!t][j][i2] != -1)
              ans = max(ans, abs(i - j) + abs(pos[t][i][i1] - pos[!t][j][i2]));
    }
  printf("%d\n", ans);
  return 0;
}
