#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, siz = 2e3 + 5;
long long t, n, s, m, k, mx_dist[10][5], change[10][5], pre_change[10][5];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k >> s;
  for (long long i = 1; i <= k; i++) {
    mx_dist[i][1] = 1e9;
    mx_dist[i][2] = -1e9;
    mx_dist[i][3] = 1e9;
    mx_dist[i][4] = -1e9;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      long long x;
      cin >> x;
      mx_dist[x][1] = min(i + j, mx_dist[x][1]);
      mx_dist[x][2] = max(i + j, mx_dist[x][2]);
      mx_dist[x][3] = min(i - j, mx_dist[x][3]);
      mx_dist[x][4] = max(i - j, mx_dist[x][4]);
    }
  }
  long long ans = 0, last_typ, typ;
  cin >> last_typ;
  for (long long i = 2; i <= s; i++) {
    cin >> typ;
    ans = max(ans, abs(mx_dist[typ][1] - mx_dist[last_typ][2]));
    ans = max(ans, abs(mx_dist[typ][2] - mx_dist[last_typ][1]));
    ans = max(ans, abs(mx_dist[typ][3] - mx_dist[last_typ][4]));
    ans = max(ans, abs(mx_dist[typ][4] - mx_dist[last_typ][3]));
    last_typ = typ;
  }
  cout << ans << endl;
}
