#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 5;
int n, m;
char dis[N][N];
int si, sj;
queue<int> x;
queue<int> y;
int d[4][2];
int f[N][N];
int t1, t2;
int step;
int ans = 0;
int main() {
  d[0][0] = 1;
  d[0][1] = 0;
  d[1][0] = -1;
  d[1][1] = 0;
  d[2][0] = 0;
  d[2][1] = 1;
  d[3][0] = 0;
  d[3][1] = -1;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      f[i][j] = 1e8 + 5;
      cin >> dis[i][j];
      if (dis[i][j] == 'E') {
        si = i;
        sj = j;
      }
      if (dis[i][j] == 'S') {
        t1 = i;
        t2 = j;
      }
    }
  }
  x.push(si);
  y.push(sj);
  f[si][sj] = 0;
  while (!x.empty() && !y.empty()) {
    int xi, yi;
    xi = x.front();
    x.pop();
    yi = y.front();
    y.pop();
    int x1, y1;
    for (int i = 0; i < 4; ++i) {
      x1 = xi + d[i][0];
      y1 = yi + d[i][1];
      if (x1 <= 0 || x1 > n || y1 <= 0 || y1 > m) continue;
      if (dis[x1][y1] == 'T') continue;
      if (f[xi][yi] + 1 < f[x1][y1]) {
        f[x1][y1] = f[xi][yi] + 1;
        x.push(x1);
        y.push(y1);
      }
    }
  }
  step = f[t1][t2];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (dis[i][j] <= '9' && dis[i][j] >= '0') {
        if (f[i][j] <= step) {
          ans += dis[i][j] - '0';
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
