#include <bits/stdc++.h>
using namespace std;
string M[1111];
int dist[1111][1111];
int BFS[2][1111111];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int r, c;
int encode(int x, int y) { return x * c + y; }
void decode(int v, int* x, int* y) {
  (*x) = v / c;
  (*y) = v % c;
}
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> M[i];
  int endx, endy;
  int startx, starty;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      dist[i][j] = -1;
      if (M[i][j] == 'E') {
        endx = i;
        endy = j;
      }
      if (M[i][j] == 'S') {
        startx = i;
        starty = j;
      }
    }
  BFS[0][0] = encode(endx, endy);
  dist[endx][endy] = 0;
  int sz = 1;
  for (int iters = 0; sz > 0; iters++) {
    int nsz = 0;
    for (int i = 0; i < sz; i++) {
      int currx, curry;
      decode(BFS[iters % 2][i], &currx, &curry);
      dist[currx][curry] = iters;
      for (int d = 0; d < 4; d++) {
        int nx = currx + dx[d];
        int ny = curry + dy[d];
        if (nx >= 0 && nx < r && ny >= 0 && ny < c && M[nx][ny] != 'T' &&
            dist[nx][ny] == -1) {
          BFS[(iters + 1) % 2][nsz++] = encode(nx, ny);
          dist[nx][ny] = iters + 1;
        }
      }
    }
    sz = nsz;
  }
  int answer = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      if (dist[i][j] != -1 && dist[i][j] <= dist[startx][starty] &&
          M[i][j] >= '1' && M[i][j] <= '9')
        answer += M[i][j] - '0';
    }
  cout << answer << endl;
}
