func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, m, k, s, x, y, cur, pr, cans, tot, a[2005][2005], dp[4][2005][2005];   vector<pair<int, int> > all[10];   int ans[10][10];   int dist(int x2, int y2, int x3, int y3) { return abs(x2 - x3) + abs(y2 - y3); }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> k >> s;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> cur;         a[i][j] = cur;         all[cur].push_back({i, j});       }     }     for (int i = 1; i <= k; i++) {       for (int j = 0; j < 4; j++)         for (int z = 0; z <= n + 1; z++) {           for (int zr = 0; zr <= m + 1; zr++) {             dp[j][z][zr] = -1e9;           }         }       for (int j = 1; j <= n; j++) {         for (int z = 1; z <= m; z++) {           dp[0][j][z] = max(dp[0][j][z - 1] + 1, dp[0][j - 1][z] + 1);           if (a[j][z] == i) dp[0][j][z] = max(dp[0][j][z], 0);         }       }       for (int j = n; j >= 1; j--) {         for (int z = 1; z <= m; z++) {           dp[1][j][z] = max(dp[1][j][z - 1] + 1, dp[1][j + 1][z] + 1);           if (a[j][z] == i) dp[1][j][z] = max(dp[1][j][z], 0);         }       }       for (int j = 1; j <= n; j++) {         for (int z = m; z >= 1; z--) {           dp[2][j][z] = max(dp[2][j][z + 1] + 1, dp[2][j - 1][z] + 1);           if (a[j][z] == i) dp[2][j][z] = max(dp[2][j][z], 0);         }       }       for (int j = n; j >= 1; j--) {         for (int z = m; z >= 1; z--) {           dp[3][j][z] = max(dp[3][j][z + 1] + 1, dp[3][j + 1][z] + 1);           if (a[j][z] == i) dp[3][j][z] = max(dp[3][j][z], 0);         }       }       for (int j = i; j <= k; j++) {         cans = -1e9;         for (int z = 0; z < all[j].size(); z++) {           x = all[j][z].first;           y = all[j][z].second;           cans = max(cans, dp[0][x][y]);           cans = max(cans, dp[1][x][y]);           cans = max(cans, dp[2][x][y]);           cans = max(cans, dp[3][x][y]);         }         ans[i][j] = cans;       }     }     tot = -1e9;     for (int i = 0; i < s; i++) {       cin >> cur;       if (i) {         if (pr < cur)           tot = max(tot, ans[pr][cur]);         else           tot = max(tot, ans[cur][pr]);       }       pr = cur;     }     cout << tot;     return 0;   }   