#include <bits/stdc++.h>
using namespace std;
const int ma = 99999999;
int n, m, x, y;
int o;
int du[10001];
char c[1001][1001], cc;
int d[1001][1001];
int dd[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
struct wgh {
  int x, y;
} s, t;
bool pan(int x, int y) {
  if (x <= 0 || y <= 0) return false;
  if (x > n || y > m) return false;
  if (c[x][y] == 'T') return false;
  return true;
}
int main() {
  int i, j;
  scanf("%d%d\n", &n, &m);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) scanf("%c", &c[i][j]);
    scanf("%c", &cc);
  }
  bool flag = false;
  int u = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (c[i][j] == 'S') {
        s.x = i;
        s.y = j;
      }
      if (c[i][j] == 'E') {
        t.x = i;
        t.y = j;
      }
      d[i][j] = ma;
    }
  }
  queue<wgh> q;
  q.push(t);
  d[t.x][t.y] = 0;
  while (!q.empty()) {
    wgh u = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      x = u.x + dd[i][0];
      y = u.y + dd[i][1];
      if (pan(x, y)) {
        if (d[x][y] > d[u.x][u.y] + 1) {
          d[x][y] = d[u.x][u.y] + 1;
          wgh o;
          o.x = x;
          o.y = y;
          q.push(o);
        }
      }
    }
  }
  int ans = 0;
  int dis = d[s.x][s.y];
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (d[i][j] <= dis) {
        if (c[i][j] >= '0' && c[i][j] <= '9') {
          ans += c[i][j] - '0';
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
