#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
int n, m;
char s[22][22];
int c[22];
int d[22][22][1 << 8];
queue<pair<int, int> > q;
int st[10];
int main() {
  st[0] = 1;
  for (int i = (1); i < (10); i++) st[i] = st[i - 1] * 3;
  cin >> n >> m;
  int mx = -1;
  for (int i = (0); i < (n); i++) {
    cin >> s[i];
    for (int j = (0); j < (m); j++)
      if (isdigit(s[i][j])) s[i][j]--, mx = max(mx, s[i][j] - '0');
  }
  int tmx = mx;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++)
      if (s[i][j] == 'B') {
        mx++;
        s[i][j] = char('0' + mx);
      }
  for (int i = (0); i < (mx + 1); i++) c[i] = -1000000;
  for (int i = (0); i < (tmx + 1); i++) cin >> c[i];
  mx++;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++)
      for (int z = (0); z < (6666); z++) d[i][j][z] = -1000000;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++)
      if (s[i][j] == 'S') {
        d[i][j][0] = 0;
        q.push(make_pair(0, i * 1000 * 10000 + j * 10000 + 0));
      }
  int it = 0;
  while (!q.empty()) {
    ++it;
    pair<int, int> ppp = q.front();
    int cost = -ppp.first;
    int x = ppp.second / 1000 / 10000;
    int y = ppp.second / 10000 % 1000;
    int mask = ppp.second % 10000;
    q.pop();
    if (d[x][y][mask] != cost) continue;
    for (int dx = (-1); dx < (2); dx++)
      for (int dy = (-1); dy < (2); dy++)
        if (abs(dx) + abs(dy) == 1) {
          int nx = x + dx;
          int ny = y + dy;
          int nmask = mask;
          if (nx >= 0 && ny >= 0 && nx < n && ny < m &&
              (s[nx][ny] == 'S' || s[nx][ny] == '.')) {
            if (dy == 0) {
              if (d[nx][ny][nmask] < cost - 1) {
                d[nx][ny][nmask] = cost - 1;
                q.push(pair<int, int>(-d[nx][ny][nmask],
                                      nx * 1000 * 10000 + ny * 10000 + nmask));
              }
            } else if (dy == 1) {
              int ncost = cost - 1;
              for (int z = (nx + 1); z < (n); z++)
                if (isdigit(s[z][y])) {
                  nmask ^= (1 << (s[z][y] - '0'));
                }
              if (d[nx][ny][nmask] < ncost) {
                d[nx][ny][nmask] = ncost;
                q.push(pair<int, int>(-d[nx][ny][nmask],
                                      nx * 1000 * 10000 + ny * 10000 + nmask));
              }
            } else if (dy == -1) {
              int ncost = cost - 1;
              for (int z = (nx + 1); z < (n); z++)
                if (isdigit(s[z][ny])) {
                  nmask ^= (1 << (s[z][ny] - '0'));
                }
              if (d[nx][ny][nmask] < ncost) {
                d[nx][ny][nmask] = ncost;
                q.push(pair<int, int>(-d[nx][ny][nmask],
                                      nx * 1000 * 10000 + ny * 10000 + nmask));
              }
            }
          }
        }
  }
  int res = 0;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++)
      if (s[i][j] == 'S') {
        for (int z = (0); z < (1 << 8); z++)
          if (d[i][j][z] != -1000000) {
            int mask = z;
            if (mask < (1 << (tmx + 1))) {
              int t = 0;
              for (int jj = (0); jj < (tmx + 1); jj++)
                if (mask & (1 << jj)) t += c[jj];
              res = max(res, d[i][j][z] + t);
            }
          }
      }
  cout << res << endl;
  return 0;
}
