#include <bits/stdc++.h>
using namespace std;
double EPS = 1e-12;
struct PT {
  double x, y;
  PT() {}
  PT(double x, double y) : x(x), y(y) {}
  PT(const PT &p) : x(p.x), y(p.y) {}
  PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
  PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
  PT operator*(double c) const { return PT(x * c, y * c); }
  PT operator/(double c) const { return PT(x / c, y / c); }
};
double dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
double dist2(PT p, PT q) { return dot(p - q, p - q); }
double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
ostream &operator<<(ostream &os, const PT &p) {
  os << "(" << p.x << "," << p.y << ")";
}
bool check(const PT &a, const PT &b, const PT &c) {
  PT d = PT(c.x + 100, c.y + 1);
  if (cross(d - a, b - a) * cross(c - a, b - a) > 0) return false;
  if (cross(a - d, c - d) * cross(b - d, c - d) > 0) return false;
  return true;
}
int dp[20][20][1 << 8];
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
void bfs(int sx, int sy, int n, int m, vector<PT> &bombs, vector<PT> &treasures,
         vector<string> &v) {
  queue<pair<pair<int, int>, int> > q;
  dp[sx][sy][0] = 0;
  q.push(make_pair(make_pair(sx, sy), 0));
  int i, j;
  int nt = int((treasures).size()), nb = int((bombs).size());
  while (!q.empty()) {
    pair<pair<int, int>, int> cur = q.front();
    q.pop();
    int x = cur.first.first, y = cur.first.second;
    int msk = cur.second;
    for (i = 0; i < 4; i++) {
      int x2 = x + dx[i], y2 = y + dy[i];
      if (x2 < 0 || x2 >= n || y2 < 0 || y2 >= m) continue;
      if (v[x2][y2] != '.' && v[x2][y2] != 'S') continue;
      int tmsk = msk;
      PT a(x, y), b(x2, y2);
      for (j = 0; j < nt; j++)
        if (check(a, b, treasures[j])) tmsk ^= (1 << j);
      for (j = 0; j < nb; j++)
        if (check(a, b, bombs[j])) tmsk ^= (1 << (j + nt));
      if (dp[x2][y2][tmsk] >= 0) continue;
      dp[x2][y2][tmsk] = dp[x][y][msk] + 1;
      q.push(make_pair(make_pair(x2, y2), tmsk));
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  ;
  int m;
  cin >> m;
  ;
  memset(dp, -1, sizeof(dp));
  vector<string> v(n);
  int i, j;
  int sx, sy;
  vector<PT> bombs;
  vector<PT> treasures2(8);
  vector<int> val(8);
  int num = 0;
  for (i = 0; i < n; i++) {
    cin >> v[i];
    for (j = 0; j < m; j++) {
      if (v[i][j] == 'S')
        sx = i, sy = j;
      else if (v[i][j] == 'B')
        bombs.push_back(PT(i, j));
      else if (v[i][j] >= '1' && v[i][j] <= '8')
        treasures2[v[i][j] - '1'] = PT(i, j), num++;
    }
  }
  vector<PT> treasures;
  for (i = 0; i < num; i++) treasures.push_back(treasures2[i]);
  for (i = 0; i < num; i++) cin >> val[i];
  bfs(sx, sy, n, m, bombs, treasures, v);
  int ans = 0;
  for (i = 0; i < 1 << num; i++) {
    int tot = 0;
    for (j = 0; j < num; j++)
      if (i & (1 << j)) tot += val[j];
    int path = 1000000;
    path = dp[sx][sy][i];
    if (path < 0) continue;
    ans = max(ans, tot - path);
  }
  cout << ans << '\n';
}
