#include <bits/stdc++.h>
using namespace std;
int n, m, sx, sy, ex, ey, ans, len;
char a[1005][1005];
int vx[1000005], vy[1000005], vn[1000005];
int mx[] = {0, 0, 1, 0, -1};
int my[] = {0, 1, 0, -1, 0};
int q[3][1000005];
int dis[1005][1005];
void bfs(int f, int s) {
  int h = 1, t = 1;
  memset(q, 0, sizeof(q));
  memset(dis, -1, sizeof(dis));
  q[1][h] = f;
  q[2][h] = s;
  dis[f][s] = 0;
  while (h <= t) {
    int x = q[1][h];
    int y = q[2][h++];
    for (int i = 0; i <= 4; i++) {
      int tx = x + mx[i];
      int ty = y + my[i];
      if (tx <= 0 || ty <= 0 || tx > n || ty > m || dis[tx][ty] != -1 ||
          a[tx][ty] == 'T')
        continue;
      q[1][++t] = tx;
      q[2][t] = ty;
      dis[tx][ty] = dis[x][y] + 1;
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (a[i][j] == 'E') {
        ex = i;
        ey = j;
      }
      if (a[i][j] > '0' && a[i][j] <= '9') {
        vx[++len] = i;
        vy[len] = j;
        vn[len] = a[i][j] - '0';
      }
    }
  }
  bfs(ex, ey);
  for (int i = 1; i <= len; i++)
    if (dis[vx[i]][vy[i]] <= dis[sx][sy] && dis[vx[i]][vy[i]] != -1) {
      ans += vn[i];
    }
  cout << ans;
  return 0;
}
