#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = (int)1e9 + 7;
const int N = (int)20 + 7;
char read_char() {
  char c;
  for (;;) {
    c = getchar();
    if (c >= 'A' && c <= 'Z') return c;
  }
}
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long add(long long a, long long b) {
  a = ((a + b) % MOD + MOD) % MOD;
  return a;
}
long long mul(long long a, long long b) { return a * b % MOD; }
struct Node {
  int x, y, Tmask, Bmask;
  Node(int xx, int yy, int t, int b) {
    x = xx;
    y = yy;
    Tmask = t;
    Bmask = b;
  }
};
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int n, m, t[N], dp[N][N][1 << 8][1 << 8];
char maze[N][N];
int sx, sy;
vector<pair<int, int> > bomb, trea;
queue<Node> Q;
bool check(int x, int y) {
  return x >= 0 && y >= 0 && x < n && y < m && maze[x][y] == '.';
}
void bfs() {
  dp[sx][sy][0][0] = 0;
  Q.push(Node(sx, sy, 0, 0));
  while (!Q.empty()) {
    Node qt = Q.front();
    Q.pop();
    for (int step = (0); step < (4); ++step) {
      int tx = qt.x + dx[step], ty = qt.y + dy[step];
      if (!check(tx, ty)) continue;
      int tmask = qt.Tmask, bmask = qt.Bmask;
      if (step == 0) {
        for (int i = (0); i < ((int)(trea).size()); ++i) {
          if (qt.x < trea[i].first && qt.y == trea[i].second) tmask ^= (1 << i);
        }
        for (int i = (0); i < ((int)(bomb).size()); ++i) {
          if (qt.x < bomb[i].first && qt.y == bomb[i].second) bmask ^= (1 << i);
        }
      } else if (step == 2) {
        for (int i = (0); i < ((int)(trea).size()); ++i) {
          if (tx < trea[i].first && ty == trea[i].second) tmask ^= (1 << i);
        }
        for (int i = (0); i < ((int)(bomb).size()); ++i) {
          if (tx < bomb[i].first && ty == bomb[i].second) bmask ^= (1 << i);
        }
      }
      if (dp[tx][ty][tmask][bmask] != -1) continue;
      dp[tx][ty][tmask][bmask] = dp[qt.x][qt.y][qt.Tmask][qt.Bmask] + 1;
      Q.push(Node(tx, ty, tmask, bmask));
    }
  }
}
void Init() {
  memset(dp, -1, sizeof(dp));
  bomb.clear();
  trea.clear();
  trea.resize(8);
  int tnum = 0;
  for (int i = (0); i < (n); ++i) {
    scanf("%s", maze[i]);
    for (int j = (0); j < (m); ++j) {
      if (maze[i][j] == 'S') {
        sx = i;
        sy = j;
        maze[i][j] = '.';
      } else if (maze[i][j] == 'B') {
        bomb.push_back({i, j});
      } else if (maze[i][j] > '0' && maze[i][j] < '9') {
        int id = maze[i][j] - '0' - 1;
        ++tnum;
        trea[id].first = i;
        trea[id].second = j;
      }
    }
  }
  trea.resize(tnum);
  for (int i = (0); i < ((int)(trea).size()); ++i) scanf("%d", &t[i]);
}
int Solve() {
  bfs();
  int ans = 0;
  for (int mask = (0); mask < (1 << (int)(trea).size()); ++mask) {
    if (dp[sx][sy][mask][0] != -1) {
      int sum = 0;
      for (int i = (0); i < ((int)(trea).size()); ++i)
        if (mask & (1 << i)) sum += t[i];
      sum -= dp[sx][sy][mask][0];
      ans = max(ans, sum);
    }
  }
  return !printf("%d\n", ans);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    Solve();
  }
  return 0;
}
