#include <bits/stdc++.h>
using namespace std;
int r, c;
int dis[1234][1234];
int Q[1234567][2];
char tf[1234][1234];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void bfs(int x, int y, int cur) {
  dis[x][y] = 1;
  Q[1][0] = x, Q[1][1] = y;
  int L = 1, R = 1;
  while (L <= R) {
    int yx = Q[L][0], yy = Q[L++][1];
    for (int i = 0; i < 4; i++) {
      int tx = yx + dx[i], ty = yy + dy[i];
      if (tx >= 0 && ty >= 0 && tx < r && ty < c && tf[tx][ty] != 'T' &&
          dis[tx][ty] == 0 && (tx != x || ty != y)) {
        dis[tx][ty] = dis[yx][yy] + 1;
        Q[++R][0] = tx, Q[R][1] = ty;
      }
    }
  }
}
int main() {
  int sx, sy, ex, ey;
  cin >> r >> c;
  getchar();
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      scanf("%c", &tf[i][j]);
      if (tf[i][j] == 'E') ex = i, ey = j;
      if (tf[i][j] == 'S') sx = i, sy = j;
    }
    getchar();
  }
  bfs(ex, ey, 0);
  int u = dis[sx][sy], ans = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (tf[i][j] >= '1' && tf[i][j] <= '9' && u >= dis[i][j] &&
          dis[i][j] != 0)
        ans += tf[i][j] - '0';
    }
  }
  cout << ans << endl;
  return 0;
}
