#include <bits/stdc++.h>
using namespace std;
int r, c, rS, cS, rE, cE, rez;
char s[1005][1005];
int m[1005][1005], f[1005][1005];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
queue<int> qer, qec;
int main() {
  int i, j, rr, cc, nr, nc;
  scanf("%d %d\n", &r, &c);
  for (i = 0; i < r; ++i) scanf("%s", s[i]);
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      if (s[i][j] == 'S') {
        rS = i;
        cS = j;
      }
      if (s[i][j] == 'E') {
        rE = i;
        cE = j;
      }
    }
  m[rE][cE] = 0;
  f[rE][cE] = 1;
  qer.push(rE);
  qec.push(cE);
  while (!qer.empty()) {
    rr = qer.front();
    cc = qec.front();
    qer.pop();
    qec.pop();
    for (i = 0; i < 4; ++i) {
      nr = rr + dx[i];
      nc = cc + dy[i];
      if (nr < 0 || nr >= r || nc < 0 || nc >= c) continue;
      if (s[nr][nc] == 'T') continue;
      if (f[nr][nc]) continue;
      f[nr][nc] = 1;
      m[nr][nc] = m[rr][cc] + 1;
      qer.push(nr);
      qec.push(nc);
    }
  }
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j)
      if (s[i][j] >= '1' && s[i][j] <= '9' && f[i][j] && m[i][j] <= m[rS][cS])
        rez += s[i][j] - '0';
  printf("%d", rez);
  return 0;
}
