#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
struct point {
  int x, y;
};
int r, c, ans;
char g[1005][1005];
int dis[1005][1005];
point s, e;
void bfs() {
  queue<point> q;
  point now, nxt;
  now.x = e.x;
  now.y = e.y;
  dis[now.x][now.y] = 0;
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      nxt.x = now.x + dir[i][0];
      nxt.y = now.y + dir[i][1];
      if (nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y < c) {
        if (dis[nxt.x][nxt.y] >= 0 || g[nxt.x][nxt.y] == 'T') continue;
        dis[nxt.x][nxt.y] = dis[now.x][now.y] + 1;
        q.push(nxt);
      }
    }
  }
}
int main() {
  while (scanf("%d%d", &r, &c) != EOF) {
    for (int i = 0; i < r; i++) {
      scanf("%s", g[i]);
      for (int j = 0; j < c; j++) {
        if (g[i][j] == 'E') {
          e.x = i;
          e.y = j;
        } else if (g[i][j] == 'S') {
          s.x = i;
          s.y = j;
        }
      }
    }
    memset(dis, -1, sizeof(dis));
    bfs();
    ans = 0;
    int ds = dis[s.x][s.y];
    for (int i = 0; i < r; i++)
      for (int j = 0; j < c; j++) {
        if (g[i][j] == 'T' || g[i][j] == 'S' || g[i][j] == 'E') continue;
        if (dis[i][j] >= 0 && dis[i][j] <= ds) {
          ans += g[i][j] - '0';
        }
      }
    printf("%d\n", ans);
  }
  return 0;
}
