#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
int n, m;
int vis[1005][1005];
struct stud {
  int time, x, y;
};
int le[1005 * 1005], ri[1005 * 1005];
int k;
int step[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int judge(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m) return 1;
  return 0;
}
void bfs(int sx, int sy) {
  int i, j;
  memset(vis, 0, sizeof(vis));
  vis[sx][sy] = 1;
  struct stud cur, next;
  queue<stud> q;
  cur.x = sx;
  cur.y = sy;
  cur.time = 0;
  q.push(cur);
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      int xx = cur.x + step[i][0];
      int yy = cur.y + step[i][1];
      if (judge(xx, yy) && !vis[xx][yy] && a[xx][yy] != 'T') {
        vis[xx][yy] = cur.time + 1;
        next.x = xx;
        next.y = yy;
        next.time = cur.time + 1;
        q.push(next);
      }
    }
  }
}
int main() {
  int i, j;
  int sx, sy;
  scanf("%d%d", &n, &m);
  k = 0;
  for (i = 0; i < n; i++) {
    scanf("%s", a[i]);
    for (j = 0; j < m; j++) {
      if (a[i][j] == 'E') {
        sx = i;
        sy = j;
      } else if (a[i][j] == 'S') {
        le[0] = i;
        ri[0] = j;
      } else if (a[i][j] > '0' && a[i][j] <= '9') {
        le[++k] = i;
        ri[k] = j;
      }
    }
  }
  bfs(sx, sy);
  int ans = 0;
  for (i = 1; i <= k; i++)
    if (vis[le[i]][ri[i]] <= vis[le[0]][ri[0]] && vis[le[i]][ri[i]])
      ans += a[le[i]][ri[i]] - '0';
  printf("%d\n", ans);
  return 0;
}
