#include <bits/stdc++.h>
using namespace std;
struct conf {
  int y, x, mask;
  conf(int y, int x, int mask) : y(y), x(x), mask(mask) {}
};
int n, m;
char field[29][29];
pair<int, int> s;
pair<int, int> b[10], t[10];
int cost[10];
int bCnt, tCnt;
void ReadData() {
  bCnt = tCnt = 0;
  scanf("%d %d%*c", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s%*c", field[i]);
    for (int j = 0; j < m; j++) {
      if (field[i][j] == 'B') {
        b[bCnt].first = i;
        b[bCnt].second = j;
        bCnt++;
      } else if ((field[i][j] - '0' >= 1) && (field[i][j] - '0' <= 9)) {
        int val = field[i][j] - '0';
        tCnt = max(tCnt, val);
        t[val - 1].first = i;
        t[val - 1].second = j;
      } else if (field[i][j] == 'S') {
        s.first = i;
        s.second = j;
        field[i][j] = '.';
      }
    }
  }
  for (int i = 0; i < tCnt; i++) {
    scanf("%d%*c", &cost[i]);
  }
}
int moves[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int dist[22][22][260];
int answer;
bool check(pair<int, int> p1, pair<int, int> p2, pair<int, int> p3,
           pair<int, int> p4) {
  if (!(p2.second >= p3.second && p4.second >= p1.second &&
        p2.first >= p3.first && p4.first >= p1.first)) {
    return false;
  }
  int x1, x2, y1, y2, mul1, mul2;
  x1 = p3.second - p1.second;
  x2 = p2.second - p1.second;
  y1 = p3.first - p1.first;
  y2 = p2.first - p1.first;
  mul1 = x1 * y2 - x2 * y1;
  x1 = p4.second - p1.second;
  y1 = p4.first - p1.first;
  mul2 = x1 * y2 - x2 * y1;
  if (mul1 * mul2 > 0) {
    return false;
  }
  x1 = p1.second - p3.second;
  x2 = p4.second - p3.second;
  y1 = p1.first - p3.first;
  y2 = p4.first - p3.first;
  mul1 = x1 * y2 - x2 * y1;
  x1 = p2.second - p3.second;
  y1 = p2.first - p3.first;
  mul2 = x1 * y2 - x2 * y1;
  if (mul1 * mul2 > 0) {
    return false;
  }
  return true;
}
void Solve() {
  answer = 0;
  memset(dist, 0xFF, sizeof(dist));
  dist[s.first][s.second][0] = 0;
  queue<conf> q;
  q.push(conf(s.first, s.second, 0));
  while (!q.empty()) {
    conf current = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = current.x + moves[i][0];
      int ny = current.y + moves[i][1];
      if (!(nx >= 0 && nx < m && ny >= 0 && ny < n)) {
        continue;
      }
      if (field[ny][nx] != '.') {
        continue;
      }
      conf nxt = conf(ny, nx, current.mask);
      for (int j = 0; j < tCnt; j++) {
        if (check(t[j], make_pair(t[j].first + 1, t[j].second + 50),
                  make_pair(current.y, current.x), make_pair(nxt.y, nxt.x))) {
          nxt.mask ^= 1 << j;
        }
      }
      for (int j = 0; j < bCnt; j++) {
        if (check(b[j], make_pair(b[j].first + 1, b[j].second + 50),
                  make_pair(current.y, current.x), make_pair(nxt.y, nxt.x))) {
          nxt.mask ^= 1 << (j + tCnt);
        }
      }
      if (dist[nxt.y][nxt.x][nxt.mask] == -1) {
        dist[nxt.y][nxt.x][nxt.mask] =
            dist[current.y][current.x][current.mask] + 1;
        q.push(nxt);
      }
    }
  }
  int cnt = 1 << tCnt;
  for (int mask = 0; mask < cnt; mask++) {
    if (dist[s.first][s.second][mask] == -1) {
      continue;
    }
    int current = 0;
    for (int j = 0; j < tCnt; j++) {
      if ((mask & (1 << j)) != 0) {
        current += cost[j];
      }
    }
    answer = max(answer, current - dist[s.first][s.second][mask]);
  }
}
void WriteData() { printf("%d\n", answer); }
int main() {
  int QWE = 1;
  for (int T = 0; T < QWE; T++) {
    ReadData();
    Solve();
    WriteData();
  }
  return 0;
}
