func_code_string: #include <bits/stdc++.h>   using namespace std;   namespace IO {   inline bool read() { return true; }   template <class T1, class... T2>   bool read(T1 &a, T2 &...rest) {     return (cin >> a) ? read(rest...) : 0;   }   template <class... T>   bool read(char *a, T &...rest) {     return (cin >> a) ? read(rest...) : 0;   }   template <class... T>   bool read(char *a, char *b, T &...rest) {     return (cin >> a >> b) ? read(rest...) : 0;   }   template <class... T>   bool read(char *a, char *b, char *c, T &...rest) {     return (cin >> a >> b >> c) ? read(rest...) : 0;   }   void put() { cout <<   n ; }   template <class T1, class... T2>   void put(const T1 num, const T2... rest) {     cout << num <<    ;     put(rest...);   }   void yes() { put( YES ); }   void no() { put( NO ); }   void ff() { cout.flush(); }   }  // namespace IO   using namespace IO;   using db = double;   using vi = vector<int>;   using pii = pair<int, int>;   using ll = long long;   using cp = complex<double>;   const int mod = 998244353, inv2 = (mod + 1) / 2;   const int maxn = 1e3 + 10;   const int inf = 1e9 + 3;   short d[41][maxn][maxn];   ll n, m, k;   void prepare() {}   short a[maxn][maxn];   queue<pair<short, short> > q[41];   vector<pair<short, short> > c[41];   short fff = 2e4;   short dx[4] = {1, -1, 0, 0};   short dy[4] = {0, 0, 1, -1};   void bfs(int k, short (&d)[maxn][maxn], queue<pair<short, short> > &q) {     vi vis(44);     vis[k] = 1;     while (!q.empty()) {       auto x = q.front().first, y = q.front().second;       q.pop();       if (!vis[a[x][y]])         for (auto g : c[a[x][y]]) {           auto x1 = g.first, y1 = g.second;           if (d[x1][y1] == fff) {             d[x1][y1] = d[x][y] + 1;             q.push(make_pair(x1, y1));           }         }       vis[a[x][y]] = 1;       for (int i = 0; i < 4; i++) {         auto xx = x + dx[i], yy = y + dy[i];         if (d[xx][yy] != fff) continue;         d[xx][yy] = d[x][y] + 1;         q.push(make_pair(xx, yy));       }     }   }   void Solve(int cas = 1) {     read(n, m, k);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         for (int q = 1; q <= k; q++) {           d[q][i][j] = fff;         }         read(a[i][j]);         c[a[i][j]].emplace_back(i, j);         d[a[i][j]][i][j] = 0;         q[a[i][j]].push(make_pair(i, j));       }     }     for (int i = 1; i <= k; i++) {       bfs(1, d[i], q[i]);     }     int q;     read(q);     while (q--) {       short r1, c1, r2, c2;       read(r1, c1, r2, c2);       int min1 = abs(r1 - r2) + abs(c1 - c2);       for (int i = 1; i <= k; i++) {         min1 = min(min1, d[i][r1][c1] + 1 + d[i][r2][c2]);       }       put(min1);     }   }   void Solvem() {     int T;     read(T);     for (int i = 1; i <= T; i++) Solve(i);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     prepare();     Solve();     return 0;   }   