#include <bits/stdc++.h>
using namespace std;
const int oo = 2000000009;
const double eps = 1e-9L;
const int mx = 22;
int t, n, m;
char g[mx][mx];
int dist[mx][mx][(1 << 8)];
int obji[8], objj[8], tot;
int v[8];
struct State {
  int i, j, mask, d;
  State(int _i, int _j, int _mask, int _d) {
    i = _i;
    j = _j;
    mask = _mask;
    d = _d;
  }
};
bool intersect(int l1x1, int l1y1, int l1x2, int l1y2, int l2x1, int l2y1,
               int l2x2, int l2y2) {
  int A1 = l1y2 - l1y1, B1 = l1x1 - l1x2, A2 = l2y2 - l2y1, B2 = l2x1 - l2x2;
  int C1 = A1 * l1x1 + B1 * l1y1, C2 = A2 * l2x1 + B2 * l2y1;
  int det = A1 * B2 - A2 * B1;
  long double x = B2 * C1 - B1 * C2;
  long double y = A1 * C2 - A2 * C1;
  assert(det);
  x /= det;
  y /= det;
  det = 1;
  bool ret =
      det * min(l1x1, l1x2) - eps <= x && x <= det * max(l1x1, l1x2) + eps &&
      det * min(l1y1, l1y2) - eps <= y && y <= det * max(l1y1, l1y2) + eps;
  ret = ret && det * min(l2x1, l2x2) - eps <= x &&
        x <= det * max(l2x1, l2x2) + eps && det * min(l2y1, l2y2) - eps <= y &&
        y <= det * max(l2y1, l2y2) + eps;
  return ret;
}
int val(int mask) {
  int ret = 0;
  for (int i = 0; i < (t); ++i)
    if ((mask & (1 << i)) != 0) ret += v[i];
  return ret;
}
int di[] = {0, 0, -1, 1}, dj[] = {-1, 1, 0, 0};
int main() {
  int si, sj;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < (n); ++i) scanf("%s", g[i]);
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j)
      if (isdigit(g[i][j])) ++t;
  for (int i = 0; i < (t); ++i) scanf("%d", &v[i]);
  memset(dist, -1, sizeof(dist));
  queue<State> q;
  tot = t;
  bool start = true;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      if (g[i][j] == 'S') {
        dist[i][j][0] = 0;
        q.push(State(i, j, 0, 0));
        si = i;
        sj = j;
      }
      if (g[i][j] == 'B') {
        assert(tot < 8);
        obji[tot] = i;
        objj[tot] = j;
        ++tot;
      }
      if (isdigit(g[i][j])) {
        obji[g[i][j] - '0' - 1] = i;
        objj[g[i][j] - '0' - 1] = j;
      }
    }
  assert(tot <= 8);
  while (!q.empty()) {
    assert((int)(q.size()) <= n * m * (1 << tot));
    State cur = q.front();
    q.pop();
    int i = cur.i, j = cur.j;
    int mask = cur.mask;
    ;
    for (int d = 0; d < (4); ++d) {
      int ni = i + di[d], nj = j + dj[d];
      if (ni < 0 || ni >= n || nj < 0 || nj >= m ||
          (g[ni][nj] != 'S' && g[ni][nj] != '.'))
        continue;
      int nmask = mask;
      for (int k = 0; k < (tot); ++k) {
        if (intersect(i, j, ni, nj, obji[k], objj[k], obji[k] + 42,
                      objj[k] + 43))
          nmask ^= (1 << k);
      }
      assert(ni >= 0 && ni < mx && nj >= 0 && nj < mx && nmask >= 0 &&
             nmask < (1 << 8));
      if (dist[ni][nj][nmask] != -1) continue;
      dist[ni][nj][nmask] = cur.d + 1;
      q.push(State(ni, nj, nmask, cur.d + 1));
    }
  }
  int ans = 0;
  for (int i = 0; i < ((1 << t)); ++i)
    if (dist[si][sj][i] != -1) ans = max(ans, val(i) - dist[si][sj][i]);
  printf("%d\n", ans);
  return 0;
}
