#include <bits/stdc++.h>
using namespace std;
__inline bool nextInt(int &val) {
  char ch;
  int sgn = 1;
  while ((ch = getchar()) != EOF) {
    if (ch == '-') sgn = -1;
    if (ch >= '0' && ch <= '9') break;
  }
  if (ch == EOF) return false;
  val = (int)(ch - '0');
  while (true) {
    ch = getchar();
    if (ch >= '0' && ch <= '9') {
      val = 10 * val + (int)(ch - '0');
    } else
      break;
  }
  val *= sgn;
  return true;
}
__inline bool nextString(string &s) {
  char ch;
  while ((ch = getchar()) != EOF) {
    if (ch >= 33 && ch <= 126) break;
  }
  if (ch == EOF) return false;
  s = string(1, ch);
  while (true) {
    ch = getchar();
    if (ch >= 33 && ch <= 126) {
      s = s + string(1, ch);
    } else
      break;
  }
  return true;
}
int dr[4] = {-1, 1, 0, 0}, dc[4] = {0, 0, -1, 1};
int nr, nc, er, ec, sr, sc, vis[1888][1888];
char row[1888][1888];
bool can(int way, int cr, int cc, int &xr, int &xc) {
  xr = cr + dr[way];
  xc = cc + dc[way];
  return (xr >= 0 && xr < nr && xc >= 0 && xc < nc && row[xr][xc] != 'T');
}
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d%d", &nr, &nc);
  for (int r = 0; r < (nr); r++) scanf("%s", row[r]);
  for (int r = 0; r < (nr); r++)
    for (int c = 0; c < (nc); c++) {
      if (row[r][c] == 'E') {
        er = r;
        ec = c;
      }
      if (row[r][c] == 'S') {
        sr = r;
        sc = c;
      }
      vis[r][c] = -1;
    }
  vis[er][ec] = 0;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(er, ec));
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    int cr = cur.first, cc = cur.second, xr, xc;
    for (int way = 0; way < (4); way++)
      if (can(way, cr, cc, xr, xc)) {
        if (vis[xr][xc] == -1) {
          vis[xr][xc] = vis[cr][cc] + 1;
          q.push(pair<int, int>(xr, xc));
        }
      }
  }
  int bestT = vis[sr][sc], ret = 0;
  for (int r = 0; r < (nr); r++)
    for (int c = 0; c < (nc); c++)
      if (row[r][c] >= '1' && row[r][c] <= '9' && vis[r][c] != -1 &&
          vis[r][c] <= bestT) {
        ret += (row[r][c] - '0');
      }
  printf("%d\n", ret);
  return 0;
}
