func_code_string: #include <bits/stdc++.h>   const int INF = 1e9;   using namespace std;   int dx[] = {-1, 1, 0, 0};   int dy[] = {0, 0, -1, 1};   bool vis[1005][1005], cvis[50];   vector<pair<int, int> > cc[50];   int m, n, k, a[1005][1005], D[45][1005][1005];   void DFS(int c) {     memset(vis, 0, sizeof(vis));     memset(cvis, 0, sizeof(cvis));     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) D[c][i][j] = 4000;     queue<pair<int, int> > Q;     for (int i = 0; i < cc[c].size(); i++) {       Q.push(cc[c][i]);       vis[cc[c][i].first][cc[c][i].second] = 1;       D[c][cc[c][i].first][cc[c][i].second] = 0;     }     cvis[c] = 1;     int first, second, xx, yy;     while (!Q.empty()) {       first = Q.front().first;       second = Q.front().second;       Q.pop();       for (int j = 0; j < 4; j++) {         xx = first + dx[j];         yy = second + dy[j];         if (xx < 1 || yy < 1 || xx > m || yy > n) continue;         if (!vis[xx][yy]) {           vis[xx][yy] = 1;           D[c][xx][yy] = D[c][first][second] + 1;           Q.push(pair<int, int>(xx, yy));         }       }       if (!cvis[a[first][second]]) {         cvis[a[first][second]] = 1;         for (int i = 0; i < cc[a[first][second]].size(); i++) {           xx = cc[a[first][second]][i].first;           yy = cc[a[first][second]][i].second;           if (!vis[xx][yy]) {             vis[xx][yy] = 1;             D[c][xx][yy] = D[c][first][second] + 1;             Q.push(pair<int, int>(xx, yy));           }         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> m >> n >> k;     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) {         cin >> a[i][j];         cc[a[i][j]].push_back(pair<int, int>(i, j));       }     for (int i = 1; i <= k; i++) DFS(i);     int ans = INF, q, r1, r2, c1, c2;     cin >> q;     while (q--) {       cin >> r1 >> c1 >> r2 >> c2;       ans = abs(r2 - r1) + abs(c1 - c2);       for (int i = 1; i <= k; i++) {         ans = min(ans, D[i][r1][c1] + D[i][r2][c2] + 1);       }       cout << ans <<   n ;     }   }   