#include <bits/stdc++.h>
using namespace std;
int bst[2000][10], arr[2000][2000];
int ans[10][10], tbst[10];
int main() {
  int n, m, k, s;
  scanf("%d %d %d %d", &n, &m, &k, &s);
  for (int i = 0, _n = n; i < _n; i++)
    for (int j = 0, _n = m; j < _n; j++) scanf("%d", &arr[i][j]);
  for (int i = 0, _n = n; i < _n; i++)
    for (int j = 0, _n = 10; j < _n; j++) bst[i][j] = -1000000000;
  memset(ans, 0, sizeof ans);
  for (int j = 0, _n = m; j < _n; j++) {
    for (int i = 0, _n = 10; i < _n; i++) tbst[i] = -1000000000;
    for (int i = 0, _n = n; i < _n; i++) {
      bst[i][arr[i][j]] = max(bst[i][arr[i][j]], 0);
      for (int k = 1, _n = 10; k < _n; k++) {
        tbst[k] = max(tbst[k], bst[i][k]);
        ans[arr[i][j]][k] = ans[k][arr[i][j]] = max(ans[arr[i][j]][k], tbst[k]);
        tbst[k]++;
      }
    }
    for (int i = 0, _n = 10; i < _n; i++) tbst[i] = -1000000000;
    for (int i = n - 1; i >= 0; i--) {
      bst[i][arr[i][j]] = max(bst[i][arr[i][j]], 0);
      for (int k = 1, _n = 10; k < _n; k++) {
        tbst[k] = max(tbst[k], bst[i][k]);
        ans[arr[i][j]][k] = ans[k][arr[i][j]] = max(ans[arr[i][j]][k], tbst[k]);
        tbst[k]++;
        bst[i][k]++;
      }
    }
  }
  int res = 0, prev = -1, tmp;
  for (int i = 0, _n = s; i < _n; i++) {
    scanf("%d", &tmp);
    if (prev != -1) {
      res = max(res, ans[tmp][prev]);
    }
    prev = tmp;
  }
  printf("%d\n", res);
  return 0;
}
