#include <bits/stdc++.h>
using namespace std;
inline void MAXIMIZE(int& x, int y) {
  if (x < y) x = y;
}
inline void MINIMIZE(int& x, int y) {
  if (x > y) x = y;
}
int const maxn = 2005;
int a[maxn][maxn];
int const maxl = 15;
int d[maxl][maxl];
int gr[maxl][maxl] = {};
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, k, s;
  cin >> n >> m >> k >> s;
  cin.ignore();
  for (int i = 0; i < n; ++i) {
    string str;
    getline(cin, str);
    for (int j = 0; j < m; ++j) a[i][j] = str[2 * j] - '0';
  }
  int const inf = 1000000000;
  for (int i = 0; i < maxl; ++i)
    for (int j = 0; j < maxl; ++j) d[i][j] = j < 4 ? -inf : inf;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      MAXIMIZE(d[a[i][j]][0], i + j);
      MAXIMIZE(d[a[i][j]][1], i - j);
      MAXIMIZE(d[a[i][j]][2], -i + j);
      MAXIMIZE(d[a[i][j]][3], -i - j);
      MINIMIZE(d[a[i][j]][4], i + j);
      MINIMIZE(d[a[i][j]][5], i - j);
      MINIMIZE(d[a[i][j]][6], -i + j);
      MINIMIZE(d[a[i][j]][7], -i - j);
    }
  int last = 0;
  string str;
  getline(cin, str);
  for (int i = 0; i < s; ++i) {
    int cur = str[2 * i] - '0';
    if (i != 0) {
      gr[last][cur] = gr[cur][last] = 1;
    }
    last = cur;
  }
  int ans = 0;
  for (int i = 1; i <= k; ++i)
    for (int j = 1; j <= k; ++j)
      if (gr[i][j]) {
        for (int t = 0; t < 4; ++t) MAXIMIZE(ans, d[i][t] - d[j][t + 4]);
      }
  cout << ans << endl;
  return 0;
}
