#include <bits/stdc++.h>
using namespace std;
const int MAX = 2000 + 10;
const int NUM = 20;
int n, m, k, s;
int a[MAX][MAX], q[NUM];
vector<int> X[NUM], Y[NUM];
int check(int a, int b) {
  int ans = 0;
  ans = max(ans, X[a][X[a].size() - 1] - X[b][0]);
  ans = max(ans, Y[a][Y[a].size() - 1] - Y[b][0]);
  swap(a, b);
  ans = max(ans, X[a][X[a].size() - 1] - X[b][0]);
  ans = max(ans, Y[a][Y[a].size() - 1] - Y[b][0]);
  return ans;
}
int main() {
  int i, j;
  cin >> n >> m >> k >> s;
  for ((i) = (1); (i) <= (n); ++(i))
    for ((j) = (1); (j) <= (m); ++(j)) {
      cin >> a[i][j];
      X[a[i][j]].push_back(i + j);
      Y[a[i][j]].push_back(i - j);
    }
  for ((i) = (1); (i) <= (k); ++(i)) {
    sort(X[i].begin(), X[i].end());
    sort(Y[i].begin(), Y[i].end());
  }
  int ans = 0;
  for ((i) = (1); (i) <= (s); ++(i)) {
    cin >> q[i];
    if (i != 1) ans = max(ans, check(q[i - 1], q[i]));
  }
  cout << ans << endl;
  return 0;
}
