#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
const int INF = 2e9;
const long long LINF = 9e18;
const int MOD1 = 2e9 + 11;
const int MOD2 = 1e9 + 9;
const int MOD3 = 1e9 + 7;
const int p = 37;
const int MAXN = 2 * 1e3;
const int MAXS = 1e5;
vector<vector<int>> a(MAXN + 2, vector<int>(MAXN + 2));
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m, k, s;
  cin >> n >> m >> k >> s;
  vector<bool> ma(10, false);
  vector<vector<bool>> g(10, vector<bool>(10, false));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      ma[a[i][j]] = true;
    }
  }
  int preww = 0;
  cin >> preww;
  for (int i = 1; i < s; ++i) {
    int v;
    cin >> v;
    g[preww][v] = true;
    g[v][preww] = true;
    preww = v;
  }
  int overall_ans = 0;
  vector<vector<int>> temp(n + 2, vector<int>(m + 2, -1));
  for (int x = 1; x <= 9; ++x) {
    vector<vector<int>> dp1(n + 2, vector<int>(m + 2, -1));
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        dp1[i][j] = -1;
        if (a[i][j] == x)
          dp1[i][j] = max(0, max(dp1[i - 1][j], dp1[i][j - 1]) + 1);
        else if (dp1[i - 1][j] != -1 || dp1[i][j - 1] != -1)
          dp1[i][j] = max(dp1[i - 1][j], dp1[i][j - 1]) + 1;
        if (g[x][a[i][j]]) ans = max(ans, dp1[i][j]);
      }
    }
    for (int i = n; i >= 1; --i) {
      for (int j = 1; j <= m; ++j) {
        dp1[i][j] = -1;
        if (a[i][j] == x)
          dp1[i][j] = max(0, max(dp1[i + 1][j], dp1[i][j - 1]) + 1);
        else if (dp1[i + 1][j] != -1 || dp1[i][j - 1] != -1)
          dp1[i][j] = max(dp1[i + 1][j], dp1[i][j - 1]) + 1;
        if (g[x][a[i][j]]) ans = max(ans, dp1[i][j]);
      }
    }
    for (int i = n; i >= 1; --i) {
      for (int j = m; j >= 1; --j) {
        dp1[i][j] = -1;
        if (a[i][j] == x)
          dp1[i][j] = max(0, max(dp1[i + 1][j], dp1[i][j + 1]) + 1);
        else if (dp1[i + 1][j] != -1 || dp1[i][j + 1] != -1)
          dp1[i][j] = max(dp1[i + 1][j], dp1[i][j + 1]) + 1;
        if (g[x][a[i][j]]) ans = max(ans, dp1[i][j]);
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = m; j >= 1; --j) {
        dp1[i][j] = -1;
        if (a[i][j] == x)
          dp1[i][j] = max(0, max(dp1[i - 1][j], dp1[i][j + 1]) + 1);
        else if (dp1[i - 1][j] != -1 || dp1[i][j + 1] != -1)
          dp1[i][j] = max(dp1[i - 1][j], dp1[i][j + 1]) + 1;
        if (g[x][a[i][j]]) ans = max(ans, dp1[i][j]);
      }
    }
    overall_ans = max(overall_ans, ans);
  }
  cout << overall_ans;
  return 0;
}
