#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
using namespace std;
const int MAX = 4e3 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 2010405347;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 500;
int n, m, k, q, a, b, x[MAX][MAX], ans, sz;
bool st[10][10];
vector<pair<int, int> > v[10];
inline bool f(int a, int b, int k) {
  int x1 = max(1, a - k) - 1, x2 = min(n, a + k), y1 = max(1, b - k) - 1,
      y2 = min(m, b + k);
  return x[x2][y2] - x[x1][y2] - x[x2][y1] + x[x1][y1] != sz;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k >> q;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> a, v[a].push_back({i + j, i - j + m});
  n += m, m = n;
  cin >> a;
  for (int i = 2; i <= q; ++i) {
    cin >> b;
    if (a <= b)
      st[a][b] = 1;
    else
      st[b][a] = 1;
    a = b;
  }
  for (int cl = 1; cl <= k; ++cl)
    if (v[cl].size()) {
      memset(x, 0, sizeof x);
      for (pair<int, int> i : v[cl]) x[i.first][i.second] = 1;
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
          x[i][j] += x[i - 1][j] + x[i][j - 1] - x[i - 1][j - 1];
      sz = v[cl].size();
      for (int o = cl; o <= k; ++o)
        if (st[cl][o])
          for (pair<int, int> i : v[o])
            while (f(i.first, i.second, ans)) ++ans;
    }
  cout << ans << '\n';
  return 0;
}
