#include <bits/stdc++.h>
using namespace std;
int n, m, sr, sc, er, ec, ic, res;
char a[1000][1000];
bool vis[1000][1000];
void bfs(bool ee) {
  queue<int> q;
  if (ee) {
    q.push(sr);
    q.push(sc);
  } else {
    q.push(er);
    q.push(ec);
  }
  q.push(0);
  int r, c, cost;
  while (!q.empty()) {
    r = q.front();
    q.pop();
    c = q.front();
    q.pop();
    cost = q.front();
    q.pop();
    if (vis[r][c]) continue;
    vis[r][c] = true;
    if (a[r][c] == 'T') continue;
    if (a[r][c] == 'E' && ee) {
      ic = cost;
      return;
    }
    if (a[r][c] >= '1' && a[r][c] <= '9' && !ee && cost <= ic)
      res += a[r][c] - '0';
    if (r > 0) {
      q.push(r - 1);
      q.push(c);
      q.push(cost + 1);
    }
    if (r < n - 1) {
      q.push(r + 1);
      q.push(c);
      q.push(cost + 1);
    }
    if (c > 0) {
      q.push(r);
      q.push(c - 1);
      q.push(cost + 1);
    }
    if (c < m - 1) {
      q.push(r);
      q.push(c + 1);
      q.push(cost + 1);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sr = i;
        sc = j;
      }
      if (a[i][j] == 'E') {
        er = i;
        ec = j;
      }
    }
  }
  bfs(true);
  memset(vis, 0, sizeof(vis));
  bfs(false);
  cout << res << endl;
  return 0;
}
