func_code_string: #include <bits/stdc++.h>   #pragma GCC target( popcnt )   using namespace std;   constexpr int siz = 1 << 9 | 1, INF = 0x7f7f7f7f;   constexpr int fx[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};   int n, m, sx, sy, cnt;   long long val[9], sum[siz];   int f[21][21][siz];   char c[21][21];   pair<int, int> t[9];   queue<tuple<int, int, int> > q;   inline bool check(char c) { return c ==  .  || c ==  S ; }   inline bool check(int first, int second, int xi, int yi, int k) {     if (xi == t[k].first && yi < t[k].second) return first < xi;     if (first == t[k].first && second < t[k].second) return first > xi;     return false;   }   long long bfs();   signed main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         cin >> c[i][j];         if (c[i][j] ==  S ) {           sx = i;           sy = j;         }         if (isdigit(c[i][j])) {           cnt++;           t[c[i][j] -  0 ] = make_pair(i, j);         }       }     for (int i = 1; i <= cnt; i++) scanf( %lld , &val[i]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (c[i][j] ==  B ) {           val[++cnt] = -INF;           t[cnt] = make_pair(i, j);         }     for (int i = 1; i < (1 << cnt); i++)       for (int j = 0; j < cnt; j++)         if ((i >> j) & 1) sum[i] += val[j + 1];     printf( %lld n , bfs());     return 0;   }   long long bfs() {     long long res(0);     q.push(make_tuple(sx, sy, 0));     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         for (int k = 0; k < (1 << cnt); k++) f[i][j][k] = -1;     f[sx][sy][0] = 0;     while (!q.empty()) {       int first, second, s;       tie(first, second, s) = q.front();       q.pop();       if (first == sx && second == sy)         res = max(res, sum[s] - f[first][second][s]);       for (int i = 0; i < 4; i++) {         const int xi = first + fx[i][0];         const int yi = second + fx[i][1];         if (xi > 0 && xi <= n && yi > 0 && yi <= m && check(c[xi][yi])) {           int si = s;           for (int j = 0; j < cnt; j++)             if (check(first, second, xi, yi, j + 1)) si ^= (1 << j);           if (f[xi][yi][si] == -1) {             f[xi][yi][si] = f[first][second][s] + 1;             q.push(make_tuple(xi, yi, si));           }         }       }     }     return res;   }   