#include <bits/stdc++.h>
using namespace std;
char s[1002][1002], tag[1002][1002];
int n, m, ex, ey, fq, lq, ux[] = {0, -1, 1, 0}, uy[] = {-1, 0, 0, 1},
                          a[1002][1002], ans = 0;
pair<int, int> P[1000001], X;
void chk(int x, int y, int c) {
  if (x >= 0 && x < n && y >= 0 && y < m && s[x][y] != 'T' && tag[x][y] == 0) {
    a[x][y] = c + 1;
    P[lq++] = make_pair(x, y);
    tag[x][y] = 1;
  }
}
int main() {
  scanf("%d %d", &n, &m);
  fq = lq = 0;
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'E') ex = i, ey = j;
  P[lq++] = make_pair(ex, ey);
  tag[ex][ey] = 1;
  while (fq < lq) {
    X = P[fq++];
    for (int i = 0; i < 4; i++)
      chk(X.first + ux[i], X.second + uy[i], a[X.first][X.second]);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S') ex = i, ey = j;
  ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] >= '0' && s[i][j] <= '9' && a[i][j] <= a[ex][ey] &&
          tag[i][j] == 1) {
        ans += s[i][j] - '0';
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
