func_code_string: #include <bits/stdc++.h>   using namespace std;   const int nmax = 1005;   const int inf = 1 << 28;   int n, m, A[nmax][nmax], B[nmax][nmax], C[nmax][nmax];   char a[nmax][nmax];   int dx[] = {-1, 0, 0, 1};   int dy[] = {0, -1, 1, 0};   deque<pair<int, int> > q;   bitset<nmax> inq[nmax];   void go(int x, int y, int d[nmax][nmax]) {     for (int i = 1; i <= n; i++) inq[i] = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) d[i][j] = inf;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] == a[x][y]) {           d[i][j] = 0;           q.push_back(make_pair(i, j));           inq[i][j] = 1;         }     while (!q.empty()) {       x = q.front().first;       y = q.front().second;       q.pop_front();       inq[x][y] = 0;       for (int k = 0; k < 4; k++) {         int newx = x + dx[k];         int newy = y + dy[k];         int cost;         if (a[newx][newy] >=  1  && a[newx][newy] <=  3 )           cost = 0;         else if (a[newx][newy] ==  . )           cost = 1;         else           cost = inf;         if (d[x][y] + cost < d[newx][newy]) {           d[newx][newy] = d[x][y] + cost;           if (!inq[newx][newy]) {             q.push_back(make_pair(newx, newy));             inq[newx][newy] = 1;           }         }       }     }   }   int main() {     cin.sync_with_stdio(false);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> (a[i] + 1);     bool foundA = 0;     bool foundB = 0;     bool foundC = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++)         if (a[i][j] ==  1  && !foundA) {           go(i, j, A);           foundA = 1;         } else if (a[i][j] ==  2  && !foundB) {           go(i, j, B);           foundB = 1;         } else if (a[i][j] ==  3  && !foundC) {           go(i, j, C);           foundC = 1;         }     }     int sol = inf;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         int cost;         if (a[i][j] ==  1  || a[i][j] ==  2  || a[i][j] ==  3 )           cost = 0;         else if (a[i][j] ==  . )           cost = 2;         sol = min(sol, A[i][j] + B[i][j] + C[i][j] - cost);       }     if (sol >= inf)       cout << -1;     else       cout << sol;     return 0;   }   