#include <bits/stdc++.h>
using namespace std;
int R, C;
int map_[1000][1000];
int matr[1000][1000];
int sr, sc, fr, fc;
int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
  scanf("%d%d", &R, &C);
  memset(map_, 0, sizeof(int) * 1000 * 1000);
  for (int i = 0; i < R; i++) {
    char s[2000];
    scanf("%s", s);
    for (int j = 0; j < C; j++) {
      if (s[j] == 'T')
        map_[i][j] = -1;
      else if (s[j] == 'S') {
        fr = i;
        fc = j;
      } else if (s[j] == 'E') {
        sr = i;
        sc = j;
      } else
        map_[i][j] = s[j] - '0';
    }
  }
  queue<pair<int, int> > q;
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++) matr[i][j] = 1000000000;
  matr[sr][sc] = 0;
  q.push(make_pair(sr, sc));
  while (!q.empty()) {
    int r = q.front().first;
    int c = q.front().second;
    q.pop();
    for (int d = 0; d < 4; d++) {
      int rr = r + dirs[d][0];
      int cc = c + dirs[d][1];
      if (rr >= 0 && rr < R && cc >= 0 && cc < C && map_[rr][cc] != -1 &&
          matr[rr][cc] > matr[r][c] + 1) {
        matr[rr][cc] = matr[r][c] + 1;
        q.push(make_pair(rr, cc));
      }
    }
  }
  int t = matr[fr][fc];
  int res = 0;
  for (int i = 0; i < R; i++)
    for (int j = 0; j < C; j++)
      if (matr[i][j] <= t) res += map_[i][j];
  printf("%d\n", res);
  return 0;
}
