#include <bits/stdc++.h>
using namespace std;
int n, m, k, s, arr[4005][4005] = {}, x = 2000, ex[9][4], song[100005], res = 0;
int main() {
  cin >> n >> m >> k >> s;
  for (int i = 0; i < 9; i++) {
    ex[i][0] = -999999999;
    ex[i][1] = 999999999;
    ex[i][2] = -999999999;
    ex[i][3] = 999999999;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i + j][i - j + x];
      int c = arr[i + j][i - j + x] - 1;
      for (int k = 0; k < 9; k++) {
        ex[c][0] = max(ex[c][0], i + j);
        ex[c][1] = min(ex[c][1], i + j);
        ex[c][2] = max(ex[c][2], i - j + x);
        ex[c][3] = min(ex[c][3], i - j + x);
      }
    }
  }
  for (int i = 0; i < s; i++) cin >> song[i], song[i]--;
  for (int i = 1; i < s; i++) {
    int a = song[i - 1], b = song[i];
    res = max(res, max(max(ex[a][0] - ex[b][1], ex[b][0] - ex[a][1]),
                       max(ex[a][2] - ex[b][3], ex[b][2] - ex[a][3])));
  }
  cout << res;
}
