#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
struct Arr {
  vector<vector<vector<vector<int> > > > a;
  Arr() {}
  Arr(int s0, int s1, int s2, int s3, int v = 0) {
    a.resize(s0);
    for (int(i) = 0; (i) < (int)(s0); ++(i)) {
      a[i].resize(s1);
      for (int(j) = 0; (j) < (int)(s1); ++(j)) {
        a[i][j].resize(s2);
        for (int(k) = 0; (k) < (int)(s2); ++(k)) a[i][j][k].assign(s3, v);
      }
    }
  }
  vector<vector<vector<int> > > &operator[](int i) { return a[i]; }
} dis;
struct Point {
  int x, y;
  Point() {}
  Point(int x, int y) : x(x), y(y) {}
};
using Vector = Point;
Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
struct stateNode {
  Point p;
  int tmask, bmask;
};
int n, m, V[10], tcnt, bcnt;
char G[25][25];
inline bool checkok(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && G[x][y] != '#' &&
         G[x][y] != 'B' && !isdigit(G[x][y]);
}
inline bool cross(Point p, Point l1, Point l2) {
  return (l1.y > p.y) != (l2.y > p.y) &&
         p.x * (l2.y - l1.y) * (l2.y - l1.y) <
             (l2.x - l1.x) * (p.y - l1.y) * (l2.y - l1.y) +
                 l1.x * (l2.y - l1.y) * (l2.y - l1.y);
}
inline int getvalue(int mask) {
  int res = 0;
  for (int(i) = 0; (i) < (int)(tcnt); ++(i))
    if (mask >> i & 1) res += V[i];
  return res;
}
void dd(int mask, int tt) {
  for (int(i) = (int)(tt - 1); (i) >= (int)(0); --(i))
    printf("%d ", mask >> i & 1);
}
int main(int argc, char const *argv[]) {
  cin >> n >> m;
  for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> G[i];
  Point st(-1, -1);
  vector<Point> tp(10), bp;
  for (int(i) = 0; (i) < (int)(n); ++(i))
    for (int(j) = 0; (j) < (int)(m); ++(j)) {
      if (G[i][j] == 'B') ++bcnt, bp.push_back(Point(i, j));
      if (isdigit(G[i][j]))
        tcnt = max(tcnt, (int)(G[i][j] - '0')), tp[G[i][j] - '1'] = Point(i, j);
      if (G[i][j] == 'S') st = Point(i, j);
    }
  for (int(i) = 0; (i) < (int)(tcnt); ++(i)) cin >> V[i];
  dis = Arr(n, m, 1 << tcnt, 1 << bcnt, inf);
  dis[st.x][st.y][0][0] = 0;
  queue<stateNode> q;
  q.push((stateNode){st, 0, 0});
  while (!q.empty()) {
    stateNode u = q.front();
    q.pop();
    int x = u.p.x, y = u.p.y, tmask = u.tmask, bmask = u.bmask,
        curdis = dis[x][y][tmask][bmask];
    for (int(k) = 0; (k) < (int)(4); ++(k)) {
      int xx = x + dx[k], yy = y + dy[k], ntmask = tmask, nbmask = bmask;
      if (!checkok(xx, yy)) continue;
      for (int(i) = 0; (i) < (int)(tcnt); ++(i))
        if (cross(tp[i], Point(x, y), Point(xx, yy))) ntmask ^= 1 << i;
      for (int(i) = 0; (i) < (int)(bcnt); ++(i))
        if (cross(bp[i], Point(x, y), Point(xx, yy))) nbmask ^= 1 << i;
      if (dis[xx][yy][ntmask][nbmask] > curdis + 1) {
        dis[xx][yy][ntmask][nbmask] = curdis + 1;
        q.push((stateNode){Point(xx, yy), ntmask, nbmask});
      }
    }
  }
  int res = 0;
  for (int(i) = 0; (i) < (int)(1 << tcnt); ++(i))
    if (dis[st.x][st.y][i][0] != inf) {
      res = max(res, getvalue(i) - dis[st.x][st.y][i][0]);
    }
  cout << res << "\n";
  return 0;
}
