#include <bits/stdc++.h>
using namespace std;
template <class _T>
inline _T sqr(const _T& first) {
  return first * first;
}
template <class _T>
inline string tostr(const _T& a) {
  ostringstream os("");
  os << a;
  return os.str();
}
const long double PI = 3.1415926535897932384626433832795L;
const double EPS = 1 - 9;
char TEMPORARY_CHAR;
const int INF = 1e9;
inline void input(int& a) {
  a = 0;
  while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') &&
         (TEMPORARY_CHAR != '-')) {
  }
  char neg = 0;
  if (TEMPORARY_CHAR == '-') {
    neg = 1;
    TEMPORARY_CHAR = getchar();
  }
  while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') {
    a = 10 * a + TEMPORARY_CHAR - '0';
    TEMPORARY_CHAR = getchar();
  }
  if (neg) a = -a;
}
inline void out(int a) {
  if (!a) putchar('0');
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  char s[10];
  int i;
  for (i = 0; a; ++i) {
    s[i] = '0' + a % 10;
    a /= 10;
  }
  for (int j = (i)-1; j >= 0; j--) putchar(s[j]);
}
inline int nxt() {
  int(ret);
  input((ret));
  ;
  return ret;
}
using namespace std;
int main() {
  int(n);
  input((n));
  ;
  int(m);
  input((m));
  ;
  int(k);
  input((k));
  ;
  int(s);
  input((s));
  ;
  short a[n][m];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) a[i][j] = nxt() - 1;
  }
  short dp[k][k];
  memset(dp, 255, sizeof(dp));
  short dp1[n][m][k], dp2[n][m][k];
  memset(dp1, 255, sizeof(dp1));
  memset(dp2, 255, sizeof(dp2));
  for (int i = 0; i < n; ++i) {
    if (i) {
      memcpy(dp1[i], dp1[i - 1], sizeof(dp1[i]));
      memcpy(dp2[i], dp2[i - 1], sizeof(dp2[i]));
    }
    for (int j = 0; j < m; ++j) {
      for (int v = 0; v < k; ++v) {
        if (dp1[i][j][v] != -1) ++dp1[i][j][v];
        if (dp2[i][j][v] != -1) ++dp2[i][j][v];
      }
    }
    for (int j = 0; j < m; ++j) {
      int c = a[i][j];
      if (dp1[i][j][c] == -1) dp1[i][j][c] = 0;
      for (int v = 0; v < k; ++v) {
        if (j)
          if (dp1[i][j - 1][v] != -1)
            ((dp1[i][j][v]) = (dp1[i][j - 1][v] + 1) > (dp1[i][j][v])
                                  ? (dp1[i][j - 1][v] + 1)
                                  : (dp1[i][j][v]));
        ((dp[c][v]) =
             (dp1[i][j][v]) > (dp[c][v]) ? (dp1[i][j][v]) : (dp[c][v]));
        ((dp[v][c]) =
             (dp1[i][j][v]) > (dp[v][c]) ? (dp1[i][j][v]) : (dp[v][c]));
      }
    }
    for (int j = m - 1; j >= 0; --j) {
      int c = a[i][j];
      if (dp2[i][j][c] == -1) dp2[i][j][c] = 0;
      for (int v = 0; v < k; ++v) {
        if (j != m - 1)
          if (dp2[i][j + 1][v] != -1)
            ((dp2[i][j][v]) = (dp2[i][j + 1][v] + 1) > (dp2[i][j][v])
                                  ? (dp2[i][j + 1][v] + 1)
                                  : (dp2[i][j][v]));
        ((dp[c][v]) =
             (dp2[i][j][v]) > (dp[c][v]) ? (dp2[i][j][v]) : (dp[c][v]));
        ((dp[v][c]) =
             (dp2[i][j][v]) > (dp[v][c]) ? (dp2[i][j][v]) : (dp[v][c]));
      }
    }
  }
  int ans = 0;
  int ss[s];
  for (int i = 0; i < s; ++i) {
    ss[i] = nxt() - 1;
    if (i) {
      ((ans) = (dp[ss[i]][ss[i - 1]]) > (ans) ? (dp[ss[i]][ss[i - 1]]) : (ans));
    }
  }
  cout << ans << endl;
  return 0;
}
