#include <bits/stdc++.h>
using namespace std;
int r, c;
queue<pair<int, int> > Q;
bool tree[1000][1000];
int counts[1000][1000];
int best[1000][1000];
void addP(int i, int j, int L) {
  if (i < 0 || j < 0 || i >= r || j >= c) return;
  if (tree[i][j]) return;
  if (best[i][j] < 1000000000) return;
  best[i][j] = L;
  Q.push(make_pair(i, j));
}
int main() {
  cin >> r >> c;
  char C;
  int ur, uc;
  int er, ec;
  string s;
  for (int i = 0; i < r; i++) {
    cin >> s;
    for (int j = 0; j < c; j++) {
      C = s[j];
      best[i][j] = 1000000000;
      tree[i][j] = false;
      counts[i][j] = 0;
      if (C == 'T')
        tree[i][j] = true;
      else if (C == 'S') {
        ur = i;
        uc = j;
      } else if (C == 'E') {
        er = i;
        ec = j;
        best[i][j] = 0;
      } else
        counts[i][j] = C - '0';
    }
  }
  Q.push(make_pair(er, ec));
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    addP(p.first + 1, p.second, best[p.first][p.second] + 1);
    addP(p.first - 1, p.second, best[p.first][p.second] + 1);
    addP(p.first, p.second + 1, best[p.first][p.second] + 1);
    addP(p.first, p.second - 1, best[p.first][p.second] + 1);
  }
  int D = best[ur][uc];
  int ans = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (best[i][j] <= D) ans += counts[i][j];
    }
  }
  cout << ans << endl;
}
