#include <bits/stdc++.h>
using namespace std;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int dx2[] = {+1, -1, 0, 0, +1, +1, -1, -1};
int dy2[] = {0, 0, +1, -1, +1, -1, -1, +1};
long long squ(long long x) { return (x * x); }
long long power(long long n, long long k) {
  long long N = n;
  if (k == 0) return 1;
  for (long long i = 2; i <= k; i++) N *= n;
  return N;
}
int r, c;
int dst[1111][1111], x[1111111], y[1111111];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int i, j;
  char s[1111][1111];
  cin >> r >> c;
  for (i = 0; i < r; i++) cin >> s[i];
  int thr = -1;
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      dst[i][j] = 1000000000;
      if (s[i][j] == 'E') {
        x[1] = i;
        y[1] = j;
        dst[i][j] = 0;
      }
    }
  }
  int b = 1, e = 1, la, ya;
  while (b <= e) {
    for (j = 0; j < 4; j++) {
      la = x[b] + dx[j];
      ya = y[b] + dy[j];
      if (la > -1 && ya > -1 && la < r && ya < c) {
        if (s[la][ya] != 'T' && dst[la][ya] == 1000000000) {
          e++;
          x[e] = la;
          y[e] = ya;
          dst[la][ya] = dst[x[b]][y[b]] + 1;
        }
      }
    }
    b++;
  }
  int ans = 0;
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++)
      if (s[i][j] == 'S') thr = dst[i][j];
  }
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      if (s[i][j] >= '0' && s[i][j] <= '9') {
        if (dst[i][j] <= thr) ans += (s[i][j] - '0');
      }
    }
  }
  cout << ans;
  return 0;
}
