#include <bits/stdc++.h>
using namespace std;
const int N1 = 1001;
char g[N1][N1];
int ed[N1][N1];
int r, c;
const int INF = N1 * N1;
bool f[N1][N1] = {{false}};
struct mv_t {
  int d, r, c;
};
void ped() {
  int i = -1;
  while (++i < r) {
    int j = -1;
    while (++j < c) {
      printf("%-11d ", ed[i][j]);
    }
    printf("\n");
  }
}
int main() {
  scanf("%d%d", &r, &c);
  int i = -1;
  mv_t ev;
  ev.d = -1;
  while (++i < r) {
    scanf("%s", g[i]);
    int j = -1;
    while (++j < c) {
      ed[i][j] = INF;
      if (g[i][j] == 'E') {
        ev.d = 0;
        ev.r = i, ev.c = j;
      }
    }
  }
  queue<mv_t> q;
  f[ev.r][ev.c] = true;
  q.push(ev);
  int x[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  int sd = INF;
  while (!q.empty()) {
    ev = q.front();
    q.pop();
    mv_t v;
    v.d = ev.d + 1;
    i = -1;
    while (++i < 4) {
      v.r = ev.r + x[i][0];
      v.c = ev.c + x[i][1];
      if (!(v.r > -1 && v.r < r && v.c > -1 && v.c < c && !f[v.r][v.c]))
        continue;
      f[v.r][v.c] = true;
      const char c = g[v.r][v.c];
      if (c == 'S')
        sd = v.d;
      else if (c > '0' && c <= '9')
        ed[v.r][v.c] = v.d;
      else if (c == 'T')
        continue;
      q.push(v);
    }
  }
  i = -1;
  int mb = 0;
  while (++i < r) {
    int j = -1;
    while (++j < c) {
      const char c = g[i][j];
      if (c > '0' && c <= '9' && ed[i][j] <= sd) {
        mb += (c - '0');
      }
    }
  }
  printf("%d\n", mb);
  return 0;
}
