#include <bits/stdc++.h>
using namespace std;
int R, C;
char a[21][21];
int numberbomb;
int numberTreasure;
int D;
int ans;
int V[10];
pair<int, int> line[10];
int dC[4] = {0, -1, 1, 0};
int dR[4] = {-1, 0, 0, 1};
int dp[21][21][512];
struct A {
  int r;
  int c;
  int number;
};
void Inite(int startR, int startC) {
  int m = 1 << D;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < m; k++) {
        dp[i][j][k] = 2000000000;
      }
    }
  }
  dp[startR][startC][0] = 0;
}
int Issur(int nr, int nc, int tr, int tc, int crtFlag) {
  for (int i = 0; i < D; i++) {
    if (tr == line[i].first && tc < line[i].second && nr < tr) {
      crtFlag ^= 1 << i;
    } else if (nr == line[i].first && nc < line[i].second && tr < nr) {
      crtFlag ^= 1 << i;
    }
  }
  return crtFlag;
}
void Solve(int startR, int startC) {
  A tempA;
  tempA.c = startC;
  tempA.r = startR;
  tempA.number = 0;
  queue<A> que;
  que.push(tempA);
  while (!que.empty()) {
    int c = que.front().c;
    int r = que.front().r;
    int crtFlag = que.front().number;
    que.pop();
    for (int k = 0; k < 4; k++) {
      if (r + dR[k] >= 0 && r + dR[k] < R && c + dC[k] >= 0 && c + dC[k] < C &&
          a[r + dR[k]][c + dC[k]] == '.') {
        int nextFlag = Issur(r, c, r + dR[k], c + dC[k], crtFlag);
        int value1 = dp[r][c][crtFlag];
        if (dp[r + dR[k]][c + dC[k]][nextFlag] > value1 + 1) {
          dp[r + dR[k]][c + dC[k]][nextFlag] = value1 + 1;
          tempA.c = c + dC[k];
          tempA.r = r + dR[k];
          tempA.number = nextFlag;
          que.push(tempA);
        }
      }
    }
  }
  int m = 1 << numberTreasure;
  for (int i = 0; i < m; i++) {
    int value1 = -dp[startR][startC][i];
    for (int j = 0; j < D; j++) {
      if ((i & (1 << j)) != 0) {
        value1 += V[j];
      }
    }
    ans = max(ans, value1);
  }
}
int main() {
  scanf("%d%d", &R, &C);
  for (int i = 0; i < R; i++) {
    scanf("%s", a[i]);
  }
  numberbomb = 0;
  numberTreasure = 0;
  int startR;
  int startC;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      if (a[i][j] >= '1' && a[i][j] <= '9') {
        numberTreasure = max(numberTreasure, a[i][j] - '0');
        a[i][j]--;
      } else if (a[i][j] == 'B')
        numberbomb++;
      else if (a[i][j] == 'S') {
        startR = i;
        startC = j;
        a[i][j] = '.';
      }
    }
  }
  D = numberbomb + numberTreasure;
  for (int i = 0; i < numberTreasure; i++) {
    scanf("%d", &V[i]);
  }
  numberbomb = 0;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      if (a[i][j] >= '0' && a[i][j] <= '9') {
        line[a[i][j] - '0'].first = i;
        line[a[i][j] - '0'].second = j;
      } else if (a[i][j] == 'B') {
        line[numberbomb + numberTreasure].first = i;
        line[numberbomb + numberTreasure].second = j;
        numberbomb++;
      }
    }
  }
  ans = 0;
  Inite(startR, startC);
  Solve(startR, startC);
  printf("%d\n", ans);
  return 0;
}
