#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int base = 311;
const int N = 2e3 + 5;
int s, n, m, k;
int q[100005], a[N][N], f[N][N][10], dist[15][15];
void solved() {
  cin >> n >> m >> k >> s;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> a[i][j];
  for (int i = 1; i <= s; ++i) cin >> q[i];
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= m; ++j)
      for (int c = 1; c <= k; ++c) f[i][j][c] = 1e9;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int c = 1; c <= k; ++c)
        f[i][j][c] = min({f[i][j][c], f[i - 1][j][c], f[i][j - 1][c]});
      f[i][j][a[i][j]] = min(f[i][j][a[i][j]], i + j);
      for (int c = 1; c <= k; ++c) {
        int mx = i + j - f[i][j][c];
        dist[c][a[i][j]] = max(dist[c][a[i][j]], mx);
        dist[a[i][j]][c] = max(dist[a[i][j]][c], mx);
      }
    }
  }
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= m; ++j)
      for (int c = 1; c <= k; ++c) f[i][j][c] = 1e9;
  for (int i = 1; i <= n; ++i) {
    for (int j = m; j >= 1; --j) {
      for (int c = 1; c <= k; ++c)
        f[i][j][c] = min({f[i][j][c], f[i - 1][j][c], f[i][j + 1][c]});
      f[i][j][a[i][j]] = min(f[i][j][a[i][j]], i - j);
      for (int c = 1; c <= k; ++c) {
        int mx = i - j - f[i][j][c];
        dist[c][a[i][j]] = max(dist[c][a[i][j]], mx);
        dist[a[i][j]][c] = max(dist[a[i][j]][c], mx);
      }
    }
  }
  int ans = 0;
  for (int i = 2; i <= s; ++i) ans = max(ans, dist[q[i]][q[i - 1]]);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("codeforces"
            ".inp",
            "r")) {
    freopen(
        "codeforces"
        ".inp",
        "r", stdin);
    freopen(
        "codeforces"
        ".out",
        "w", stdout);
  }
  solved();
}
