func_code_string: #include <bits/stdc++.h>   using namespace std;   int ar[305][305];   vector<pair<int, int> > poss[90005];   int dp[305][305];   vector<int> col[305];   int vis[305];   int n, m, p;   void pr() {}   void solve() {     memset(dp, 127, sizeof(dp));     ;     scanf( %d %d %d , &n, &m, &p);     for (int i = 0; i < (int)(n); i++) {       for (int j = 0; j < (int)(m); j++) {         scanf( %d , &ar[i][j]);         poss[ar[i][j]].push_back(make_pair(i, j));         if (ar[i][j] == 1) {           dp[i][j] = i + j;         }       }     }     for (int val = 2; val <= p; val++) {       memset(vis, 0, sizeof(vis));       ;       for (int i = 0; i < (int)(m); i++) {         col[i].clear();       }       for (int i = 0; i < poss[val].size(); i++) {         pair<int, int> a = poss[val][i];         col[a.second].push_back(a.first);       }       for (int i = 0; i < poss[val - 1].size(); i++) {         pair<int, int> a = poss[val - 1][i];         vis[a.first] = 1;       }       for (int i = 0; i < n; i++) {         if (!vis[i]) continue;         int best = 1e9;         for (int j = 0; j < m; j++) {           best++;           if (ar[i][j] == val - 1) best = min(best, dp[i][j]);           for (int k = 0; k < col[j].size(); k++) {             dp[col[j][k]][j] = min(dp[col[j][k]][j], best + abs(col[j][k] - i));           }         }         best = 1e9;         for (int j = m; j >= 0; j--) {           best++;           if (ar[i][j] == val - 1) best = min(best, dp[i][j]);           for (int k = 0; k < col[j].size(); k++) {             dp[col[j][k]][j] = min(dp[col[j][k]][j], best + abs(col[j][k] - i));           }         }       }     }     printf( %d , dp[poss[p][0].first][poss[p][0].second]);   }   int main() {     int TC = 1;     for (int ZZ = 1; ZZ <= TC; ZZ++) {       solve();     }     return 0;   }   