func_code_string: #include <bits/stdc++.h>   using namespace std;   int const mxn = 1e3 + 10;   vector<pair<int, int> > ha[3], adj0[mxn][mxn], adj1[mxn][mxn];   int mark[mxn][mxn][3], fl[mxn][mxn][3];   int n, m;   int ans = 1e9;   string l[mxn];   void connect(int i, int j, int i2, int j2) {     if (i2 < 0 or i2 >= n or j2 < 0 or j2 >= m)       return;     else {       if (l[i2][j2] ==  . )         adj1[i][j].push_back({i2, j2});       else if (l[i2][j2] !=  # )         adj0[i][j].push_back({i2, j2});     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> l[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (l[i][j] ==  1  or l[i][j] ==  2  or l[i][j] ==  3 ) {           ha[l[i][j] -  1 ].push_back({i, j});         }         connect(i, j, i + 1, j);         connect(i, j, i - 1, j);         connect(i, j, i, j + 1);         connect(i, j, i, j - 1);       }     }     for (int now = 0; now <= 2; now++) {       queue<pair<int, int> > q;       for (int i = 0; i < ha[now].size(); i++) {         q.push(ha[now][i]);         fl[ha[now][i].first][ha[now][i].second][now] = 0;         mark[ha[now][i].first][ha[now][i].second][now] = 1;       }       while (q.size()) {         int v1, v2;         v1 = q.front().first, v2 = q.front().second;         q.pop();         queue<pair<int, int> > q0;         q0.push({v1, v2});         while (q0.size()) {           int a1, a2;           a1 = q0.front().first, a2 = q0.front().second;           q0.pop();           for (int j = 0; j < adj0[a1][a2].size(); j++) {             int u1 = adj0[a1][a2][j].first, u2 = adj0[a1][a2][j].second;             if (!mark[u1][u2][now]) {               q0.push({u1, u2});               q.push({u1, u2});               mark[u1][u2][now] = 1;               fl[u1][u2][now] = fl[v1][v2][now];             }           }         }         for (int j = 0; j < adj1[v1][v2].size(); j++) {           int k1 = adj1[v1][v2][j].first, k2 = adj1[v1][v2][j].second;           if (!mark[k1][k2][now]) {             mark[k1][k2][now] = 1;             q.push({k1, k2});             fl[k1][k2][now] = fl[v1][v2][now] + 1;           }         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (l[i][j] ==  .  and mark[i][j][0] and mark[i][j][1] and mark[i][j][2])           ans = min(ans, (fl[i][j][0] + fl[i][j][2] + fl[i][j][1]) - 2);         else if (l[i][j] !=  #  and mark[i][j][0] and mark[i][j][1] and                  mark[i][j][2])           ans = min(ans, fl[i][j][0] + fl[i][j][2] + fl[i][j][1]);       }     }     if (ans == 1e9)       cout << -1;     else       cout << ans;   }   