#include <bits/stdc++.h>
using namespace std;
int dump_char;
queue<pair<int, int> > q1, q2, q3;
int scr[1002][1002], r, c, status[1002][1002];
char arr[1002][1002];
void init() {
  int i;
  while (!q3.empty()) {
    q3.pop();
  }
  memset(scr, 0, sizeof(0));
  memset(status, 0, sizeof(0));
  for (i = 0; i <= c + 1; i++) {
    status[0][i] = status[r + 1][i] = 1;
  }
  for (i = 0; i <= r + 1; i++) {
    status[i][0] = status[i][c + 1] = 1;
  }
}
void bfs(int cnt) {
  pair<int, int> p;
  if (q1.empty()) return;
  while (!q1.empty()) {
    p = q1.front();
    q1.pop();
    if (arr[p.first][p.second] == 'T' || status[p.first][p.second]) continue;
    scr[p.first][p.second] = cnt;
    status[p.first][p.second] = 1;
    q2.push(make_pair(p.first - 1, p.second));
    q2.push(make_pair(p.first + 1, p.second));
    q2.push(make_pair(p.first, p.second - 1));
    q2.push(make_pair(p.first, p.second + 1));
  }
  q1 = q2;
  q2 = q3;
  bfs(++cnt);
}
int main() {
  int i, j, sx, sy, ex, ey, cnt = 0, temp;
  scanf("%d", &r);
  scanf("%d", &c);
  getchar();
  init();
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'E') {
        ex = i;
        ey = j;
      }
      if (arr[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
    getchar();
  }
  q1.push(make_pair(ex, ey));
  bfs(0);
  temp = scr[sx][sy];
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      if (arr[i][j] >= '1' && arr[i][j] <= '9' && status[i][j] &&
          temp >= scr[i][j]) {
        cnt += (arr[i][j] - '0');
      }
    }
  }
  printf("%d\n", cnt);
  return 0;
}
