#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
char c[1005][1005];
int d[1005][1005];
struct po {
  int x;
  int y;
};
queue<po> q;
int ex, ey, sx, sy;
void bfs() {
  int xx, yy, i, j, x, y;
  memset(d, -1, sizeof(d));
  d[ex][ey] = 0;
  q.push((po){ex, ey});
  while (!q.empty()) {
    xx = q.front().x;
    yy = q.front().y;
    q.pop();
    for (i = 0; i < 4; i++) {
      x = xx + dx[i];
      y = yy + dy[i];
      if ((c[x][y] == 'S' || (c[x][y] >= '0' && c[x][y] <= '9')) &&
          d[x][y] == -1) {
        d[x][y] = d[xx][yy] + 1;
        q.push((po){x, y});
      }
    }
  }
}
int main(void) {
  int n, m, i, j, ans = 0;
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) cin >> c[i][j];
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (c[i][j] == 'E') {
        ex = i;
        ey = j;
      }
      if (c[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  bfs();
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      if (c[i][j] != 'E' && c[i][j] != 'S' && d[i][j] != -1 &&
          d[i][j] <= d[sx][sy])
        ans += c[i][j] - '0';
    }
  }
  cout << ans;
  return 0;
}
