#include <bits/stdc++.h>
using namespace std;
const int Len = 1000;
const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Point {
  int x, y;
} pexit, pstart;
int n, m, ans, sd;
char s[Len + 5][Len + 5];
int dis[Len + 5][Len + 5];
bool visit[Len + 5][Len + 5];
queue<Point> Q;
bool check(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m) return false;
  if (s[x][y] == 'T') return false;
  return true;
}
void solve() {
  memset(dis, -1, sizeof(dis));
  dis[pexit.x][pexit.y] = 0;
  while (!Q.empty()) Q.pop();
  Q.push(pexit);
  while (!Q.empty()) {
    Point curP = Q.front(), tmpP;
    int distance = dis[curP.x][curP.y] + 1;
    Q.pop();
    visit[curP.x][curP.y] = false;
    for (int i = 0; i < 4; ++i) {
      int tx = curP.x + dir[i][0], ty = curP.y + dir[i][1];
      if (check(tx, ty)) {
        if (dis[tx][ty] == -1 || dis[tx][ty] > distance) {
          dis[tx][ty] = distance;
          tmpP.x = tx, tmpP.y = ty;
          if (!visit[tx][ty]) Q.push(tmpP), visit[tx][ty] = true;
        }
      }
    }
  }
  sd = dis[pstart.x][pstart.y];
  ans = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (!(i == pstart.x && j == pstart.y) && dis[i][j] > 0 && dis[i][j] <= sd)
        ans += (s[i][j] - '0');
  printf("%d\n", ans);
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    gets(s[0]);
    for (int i = 0; i < n; ++i) {
      gets(s[i]);
      for (int j = 0; j < m; ++j)
        if (s[i][j] == 'E')
          pexit.x = i, pexit.y = j;
        else if (s[i][j] == 'S')
          pstart.x = i, pstart.y = j;
    }
    solve();
  }
  return 0;
}
