#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
bool dead[N][N];
int r, c;
int d[N][N];
int forx[] = {0, 0, 1, -1};
int fory[] = {1, -1, 0, 0};
string t[N];
vector<pair<int, int> > check;
bool valid(int x, int y) {
  if ((x > -1) && (x < r) && (y > -1) && (y < c))
    if (t[x][y] != 'T') return true;
  return false;
}
void bfs(int x, int y) {
  dead[x][y] = true;
  d[x][y] = 0;
  check.push_back(make_pair(x, y));
  int p = 0;
  int kid1, kid2;
  int now1, now2;
  while (p != check.size()) {
    now1 = check[p].first, now2 = check[p].second;
    for (int i = 0; i < 4; ++i) {
      kid1 = now1 + forx[i];
      kid2 = now2 + fory[i];
      if (valid(kid1, kid2))
        if (!(dead[kid1][kid2])) {
          d[kid1][kid2] = d[now1][now2] + 1;
          check.push_back(make_pair(kid1, kid2));
          dead[kid1][kid2] = true;
        }
    }
    p++;
  }
}
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; ++i) cin >> t[i];
  bool tt = false;
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j)
      if (t[i][j] == 'E') {
        bfs(i, j);
        tt = true;
        break;
      }
    if (tt) break;
  }
  int ans = 0;
  int base;
  for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j)
      if (t[i][j] == 'S') base = d[i][j];
  for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j)
      if ((t[i][j] > '0') && (t[i][j] <= '9') && (d[i][j] <= base) &&
          (d[i][j] != 0)) {
        ans += (t[i][j] - '0');
      }
  cout << ans;
}
