#include <bits/stdc++.h>
using namespace std;
inline void setmin(int &x, int y) {
  if (y < x) x = y;
}
inline void setmax(int &x, int y) {
  if (y > x) x = y;
}
inline void setmin(long long &x, long long y) {
  if (y < x) x = y;
}
inline void setmax(long long &x, long long y) {
  if (y > x) x = y;
}
const int N = 2000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
int a[N][N];
int d[9][9];
int high[N][9], low[N][9];
int best[9];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int n, m, k, s;
  cin >> n >> m >> k >> s;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < k; j++) {
      high[i][j] = -inf;
      low[i][j] = inf;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      a[i][j]--;
      setmax(high[j][a[i][j]], i);
      setmin(low[j][a[i][j]], i);
    }
  for (int i = 0; i < n; i++) {
    fill(best, best + 9, -inf);
    for (int j = 0; j < m; j++) {
      for (int x = 0; x < k; x++) {
        setmax(best[x], i - low[j][x]);
        setmax(best[x], high[j][x] - i);
        setmax(d[a[i][j]][x], best[x]);
        best[x]++;
      }
    }
  }
  int tmp[s];
  for (int i = 0; i < s; i++) {
    cin >> tmp[i];
    tmp[i]--;
  }
  int ans = 0;
  for (int i = 0; i < s - 1; i++)
    setmax(ans, max(d[tmp[i]][tmp[i + 1]], d[tmp[i + 1]][tmp[i]]));
  cout << ans << "\n";
  return 0;
}
