#include <bits/stdc++.h>
using namespace std;
char b[1003][1003];
int dist[1003][1003];
int main() {
  int r, c, ei, ej;
  scanf("%d %d", &r, &c);
  for (int i = 0; i < r; i++) {
    scanf("%s", b[i]);
    for (int j = 0; j < c; j++) {
      if (b[i][j] == 'E') {
        ei = i;
        ej = j;
      }
    }
  }
  int mdist;
  vector<pair<int, int>> d;
  queue<pair<int, int>> q;
  q.push({ei, ej});
  dist[ei][ej] = 1;
  while (!q.empty()) {
    const int i = q.front().first;
    const int j = q.front().second;
    q.pop();
    for (int ni = max(0, i - 1); ni <= min(r - 1, i + 1); ni++) {
      for (int nj = max(0, j - 1); nj <= min(c - 1, j + 1); nj++) {
        if (!dist[ni][nj] && b[ni][nj] != 'T' &&
            abs(ni - i) + abs(nj - j) == 1) {
          q.push({ni, nj});
          dist[ni][nj] = dist[i][j] + 1;
          if (b[ni][nj] == 'S') {
            mdist = dist[ni][nj];
          } else {
            d.push_back({dist[ni][nj], b[ni][nj] - '0'});
          }
        }
      }
    }
  }
  sort(d.begin(), d.end());
  int ans = 0, i = 0;
  while (i < (int)d.size() && d[i].first <= mdist) {
    ans += d[i].second;
    i++;
  }
  printf("%d", ans);
  return 0;
}
