#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
const int INF = 0x3f3f3f3f;
int n, m, sum = 0, sx, sy, ex, ey, cnt = 0, shortest;
char mp[maxn][maxn];
int vis[maxn][maxn], steps[maxn][maxn], breeder[maxn * maxn][2];
int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
struct node {
  int x, y, step;
};
void BFS() {
  memset(vis, 0, sizeof(vis));
  queue<node> q;
  q.push((node){ex, ey, 0});
  while (q.size()) {
    node it = q.front();
    q.pop();
    if (it.x == sx && it.y == sy) {
      shortest = it.step;
      return;
    }
    for (int i = 0; i < 4; i++) {
      int tx = it.x + dir[i][0];
      int ty = it.y + dir[i][1];
      if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
      if (!vis[tx][ty] && mp[tx][ty] != 'T') {
        if (mp[tx][ty] > '0' && mp[tx][ty] <= '9') {
          steps[tx][ty] = it.step + 1;
        }
        vis[tx][ty] = 1;
        q.push((node){tx, ty, it.step + 1});
      }
    }
  }
}
int main() {
  memset(steps, 0x3f, sizeof(steps));
  scanf("%d%d", &n, &m);
  getchar();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      mp[i][j] = getchar();
      if (mp[i][j] == 'S') {
        sx = i;
        sy = j;
      } else if (mp[i][j] == 'E') {
        ex = i;
        ey = j;
      } else if (mp[i][j] > '0' && mp[i][j] <= '9') {
        breeder[++cnt][0] = i;
        breeder[cnt][1] = j;
      }
    }
    getchar();
  }
  BFS();
  for (int i = 1; i <= cnt; i++) {
    if (steps[breeder[i][0]][breeder[i][1]] <= shortest)
      sum += (mp[breeder[i][0]][breeder[i][1]] - '0');
  }
  printf("%d\n", sum);
  return 0;
}
