#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int N = 1100;
char edge[N][N];
struct eg {
  int x, y;
  int step;
} sat, ed, p[N * N], mid, t;
int vis[N][N];
int r, c;
void bfs() {
  int i;
  memset(vis, -1, sizeof(vis));
  vis[ed.x][ed.y] = 0;
  queue<eg> q;
  q.push(ed);
  while (!q.empty()) {
    mid = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      t.x = mid.x + dx[i];
      t.y = mid.y + dy[i];
      if (t.x >= 0 && t.x < r && t.y >= 0 && t.y < c && edge[t.x][t.y] != 'T' &&
          vis[t.x][t.y] == -1) {
        t.step = mid.step + 1;
        vis[t.x][t.y] = t.step;
        q.push(t);
      }
    }
  }
}
int main() {
  int cnt = 0, i, j;
  scanf("%d%d", &r, &c);
  for (i = 0; i < r; i++) {
    scanf("%s", edge[i]);
    for (j = 0; j < c; j++) {
      if (edge[i][j] == 'S') {
        sat.x = i;
        sat.y = j;
      }
      if (edge[i][j] == 'E') {
        ed.x = i;
        ed.y = j;
      }
      if (edge[i][j] >= '1' && edge[i][j] <= '9') {
        p[cnt].x = i;
        p[cnt].y = j;
        p[cnt].step = edge[i][j] - '0';
        cnt++;
      }
    }
  }
  bfs();
  int sum = 0;
  p[cnt].x = sat.x;
  p[cnt].y = sat.y;
  for (i = 0; i < cnt; i++)
    if (vis[p[i].x][p[i].y] != -1 &&
        vis[p[i].x][p[i].y] <= vis[p[cnt].x][p[cnt].y])
      sum += p[i].step;
  printf("%d\n", sum);
  return 0;
}
