#include <bits/stdc++.h>
using namespace std;
int n, a[1111][1111], q[1111 * 1111][3], d[1111][1111], ii, jj, sx, sy, x, y,
    qe, qb, m;
bool was[1111][1111] = {false};
inline void put(int i, int j) {
  if (i > n || i < 1 || j > m || j < 1 || a[i][j] == -1) return;
  if (was[i][j]) return;
  was[i][j] = true;
  d[i][j] = d[x][y] + 1;
  qe++;
  q[qe][0] = i;
  q[qe][1] = j;
}
int main(int argc, char* argv[]) {
  scanf("%d", &n);
  scanf("%d\n", &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char ch = getchar();
      if (ch == '0')
        a[i][j] = 0;
      else if (ch == 'T')
        a[i][j] = -1;
      else if (ch == 'E')
        sx = i, sy = j, a[i][j] = 0;
      else if (ch >= '0' && ch <= '9')
        a[i][j] = ch - '0';
      else if (ch == 'S')
        ii = i, jj = j;
    }
    getchar();
  }
  memset(d, 63, sizeof(d));
  qb = 1;
  qe = 0;
  put(sx, sy);
  d[sx][sy] = 0;
  while (qe >= qb) {
    x = q[qb][0], y = q[qb][1];
    qb++;
    put(x, y + 1);
    put(x, y - 1);
    put(x + 1, y);
    put(x - 1, y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (d[i][j] <= d[ii][jj]) ans += a[i][j];
  cout << ans << endl;
  return 0;
}
