#include <bits/stdc++.h>
using namespace std;
const int N = 2001;
int dp[N][N][10], a[N][N], ans[10][10];
int c[100005];
int main() {
  int n, m, k, s;
  int i, j, kk;
  scanf("%d%d%d%d", &n, &m, &k, &s);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) scanf("%d", &a[i][j]);
  }
  memset(dp, -1, sizeof(dp));
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      dp[i][j][a[i][j]] = 0;
      for (kk = 1; kk <= k; kk++) {
        if (i > 0 && dp[i - 1][j][kk] != -1)
          dp[i][j][kk] = max(dp[i][j][kk], 1 + dp[i - 1][j][kk]);
        if (j > 0 && dp[i][j - 1][kk] != -1)
          dp[i][j][kk] = max(dp[i][j][kk], dp[i][j - 1][kk] + 1);
        if (i > 0 && j > 0 && dp[i - 1][j - 1][kk] != -1)
          dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - 1][kk] + 2);
        if (dp[i][j][kk] != -1)
          ans[a[i][j]][kk] = max(ans[a[i][j]][kk], dp[i][j][kk]);
        ans[kk][a[i][j]] = ans[a[i][j]][kk];
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  for (i = 0; i < n; i++) {
    for (j = m - 1; j >= 0; j--) {
      dp[i][j][a[i][j]] = 0;
      for (kk = 1; kk <= k; kk++) {
        if (i > 0 && dp[i - 1][j][kk] != -1)
          dp[i][j][kk] = max(dp[i][j][kk], 1 + dp[i - 1][j][kk]);
        if (j + 1 < m && dp[i][j + 1][kk] != -1)
          dp[i][j][kk] = max(dp[i][j][kk], dp[i][j + 1][kk] + 1);
        if (i > 0 && j + 1 < m && dp[i - 1][j + 1][kk] != -1)
          dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j + 1][kk] + 2);
        if (dp[i][j][kk] != -1)
          ans[a[i][j]][kk] = max(ans[a[i][j]][kk], dp[i][j][kk]);
        ans[kk][a[i][j]] = ans[a[i][j]][kk];
      }
    }
  }
  int o = 0;
  for (i = 0; i < s; i++) {
    scanf("%d", &c[i]);
    if (i > 0) {
      o = max(o, ans[c[i - 1]][c[i]]);
    }
  }
  printf("%d", o);
}
