You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.

You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, enter the cells with treasures, obstacles and bombs. To pick the treasures, you need to build a closed path (starting and ending in the starting cell). The closed path mustn't contain any cells with bombs inside. Let's assume that the sum of the treasures' values that are located inside the closed path equals v, and besides, you've made k single moves (from one cell to another) while you were going through the path, then such path brings you the profit of v - k rubles.

Your task is to build a closed path that doesn't contain any bombs and brings maximum profit.

Note that the path can have self-intersections. In order to determine if a cell lies inside a path or not, use the following algorithm:

  1. Assume that the table cells are points on the plane (the table cell on the intersection of the i-th column and the j-th row is point (i, j)). And the given path is a closed polyline that goes through these points. 
  2. You need to find out if the point p of the table that is not crossed by the polyline lies inside the polyline. 
  3. Let's draw a ray that starts from point p and does not intersect other points of the table (such ray must exist). 
  4. Let's count the number of segments of the polyline that intersect the painted ray. If this number is odd, we assume that point p (and consequently, the table cell) lie inside the polyline (path). Otherwise, we assume that it lies outside. 

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 20) — the sizes of the table. Next n lines each contains m characters — the description of the table. The description means the following:

  * character "B" is a cell with a bomb; 
  * character "S" is the starting cell, you can assume that it's empty; 
  * digit c (1-8) is treasure with index c; 
  * character "." is an empty cell; 
  * character "#" is an obstacle. 



Assume that the map has t treasures. Next t lines contain the prices of the treasures. The i-th line contains the price of the treasure with index i, vi ( - 200 ≤ vi ≤ 200). It is guaranteed that the treasures are numbered from 1 to t. It is guaranteed that the map has not more than 8 objects in total. Objects are bombs and treasures. It is guaranteed that the map has exactly one character "S".

Output

Print a single integer — the maximum possible profit you can get.

Examples

Input

4 4
....
.S1.
....
....
10


Output

2


Input

7 7
.......
.1###2.
.#...#.
.#.B.#.
.3...4.
..##...
......S
100
100
100
100


Output

364


Input

7 8
........
........
....1B..
.S......
....2...
3.......
........
100
-100
100


Output

0


Input

1 1
S


Output

0

Note

In the first example the answer will look as follows.

<image>

In the second example the answer will look as follows.

<image>

In the third example you cannot get profit.

In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell.
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
char buf[N][N];
const int K = 8;
const int MSK = 1 << K;
struct vt {
  int x, y;
  vt(int _x, int _y) { x = _x, y = _y; }
  vt() {}
  friend int operator^(vt a, vt b) { return a.x * b.y - b.x * a.y; }
  friend vt operator-(vt a, vt b) { return vt(a.x - b.x, a.y - b.y); }
  friend vt operator+(vt a, vt b) { return vt(a.x + b.x, a.y + b.y); }
};
int T[K];
vt pos[K];
int oldi[K];
int C[K];
const int dx = 42, dy = 43;
int D[MSK][N][N];
int lpt = 0, rpt = 0;
int Q[10 * MSK * N * N];
int vx[] = {1, 0, -1, 0};
int vy[] = {0, 1, 0, -1};
int n, m;
int pt = 0;
inline int sign(int x) { return (x > 0) - (x < 0); }
inline bool inter(vt a, vt b, vt c, vt d) {
  return (sign((c - a) ^ (b - a)) * sign((d - a) ^ (b - a)) == -1 &&
          sign((a - c) ^ (d - c)) * sign((b - c) ^ (d - c)) == -1);
}
char _aff[N][N][4][K];
bool affect(int y, int x, int v, int i) {
  if (_aff[y][x][v][i] != -1)
    return _aff[y][x][v][i];
  else
    return _aff[y][x][v][i] = inter(vt(y, x), vt(y, x) + vt(vy[v], vx[v]),
                                    pos[i], pos[i] + vt(dy, dx));
}
void BFS(int sy, int sx) {
  memset(D, -1, sizeof(D));
  D[0][sy][sx] = 0;
  Q[rpt++] = 0;
  Q[rpt++] = sy;
  Q[rpt++] = sx;
  while (lpt != rpt) {
    int msk = Q[lpt++];
    int y = Q[lpt++];
    int x = Q[lpt++];
    for (int v = 0; v < 4; v++) {
      int ty = y + vy[v];
      int tx = x + vx[v];
      if (ty < 0 || tx < 0 || ty >= n || tx >= m) continue;
      if (buf[ty][tx] != '.' && buf[ty][tx] != 'S') continue;
      int tmsk = msk;
      for (int i = 0; i < pt; i++) {
        if (affect(y, x, v, i)) tmsk ^= (1 << i);
      }
      if (D[tmsk][ty][tx] != -1) continue;
      D[tmsk][ty][tx] = D[msk][y][x] + 1;
      Q[rpt++] = tmsk;
      Q[rpt++] = ty;
      Q[rpt++] = tx;
    }
  }
}
int main() {
  memset(_aff, -1, sizeof(_aff));
  scanf("%d %d ", &n, &m);
  int sx = -1, sy = -1;
  int tr = 0;
  for (int i = 0; i < n; i++) {
    gets(buf[i]);
    for (int j = 0; j < m; j++) {
      if (buf[i][j] == 'S')
        sy = i, sx = j;
      else if (buf[i][j] == 'B')
        T[pt] = 0, pos[pt] = vt(i, j), pt++;
      else if ('1' <= buf[i][j] && buf[i][j] <= '8')
        T[pt] = 1, oldi[pt] = buf[i][j] - '1', pos[pt] = vt(i, j), pt++, tr++;
    }
  }
  for (int i = 0; i < tr; i++) scanf("%d", &C[i]);
  assert(sx != -1);
  BFS(sy, sx);
  int ans = 0;
  for (int msk = 0; msk < (1 << pt); msk++) {
    if (D[msk][sy][sx] != -1) {
      int cst = 0;
      bool bad = false;
      for (int i = 0; i < pt; i++)
        if ((msk >> i) & 1) {
          if (!T[i])
            bad = true;
          else
            cst += C[oldi[i]];
        }
      if (!bad) ans = max(ans, cst - D[msk][sy][sx]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
