#include <bits/stdc++.h>
int R, C;
int ER, EC;
int SR, SC;
char field[1010][1010];
int dist[1010][1010];
int Q[1010 * 1010], Qb, Qe;
int dr[4] = {-1, +1, 0, 0};
int dc[4] = {0, 0, -1, +1};
int main(void) {
  scanf("%d %d", &R, &C);
  for (int r = 0; r < R; ++r) {
    scanf("%s", field[r]);
    for (int c = 0; c < C; ++c) {
      if (field[r][c] == 'E') {
        ER = r;
        EC = c;
      }
      if (field[r][c] == 'S') {
        SR = r;
        SC = c;
      }
    }
  }
  memset(dist, -1, sizeof dist);
  Q[0] = ER * 10000 + EC;
  Qe = 1;
  dist[ER][EC] = 0;
  while (Qb != Qe) {
    int r = Q[Qb] / 10000;
    int c = Q[Qb] % 10000;
    Qb += 1;
    for (int d = 0; d < 4; ++d) {
      int rr = r + dr[d];
      int cc = c + dc[d];
      if (rr < 0 || cc < 0 || rr >= R || cc >= C) continue;
      if (field[rr][cc] == 'T') continue;
      if (dist[rr][cc] != -1) continue;
      dist[rr][cc] = dist[r][c] + 1;
      Q[Qe++] = rr * 10000 + cc;
    }
  }
  int battles = 0;
  int mydist = dist[SR][SC];
  for (int r = 0; r < R; ++r) {
    for (int c = 0; c < C; ++c) {
      if (!isdigit(field[r][c])) continue;
      if (dist[r][c] == -1 || dist[r][c] > mydist) continue;
      int cnt = field[r][c] - '0';
      battles += cnt;
    }
  }
  printf("%d\n", battles);
  return 0;
}
