#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long int, char> p1, pair<long long int, char> p2) {
  return p1.first > p2.first;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int r, c, i, j, is, js, ie, je;
  cin >> r >> c;
  string s[r];
  for (i = 0; i < r; i++) cin >> s[i];
  queue<pair<long long int, long long int> > q;
  long long int d[r][c];
  bool vis[r][c];
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++) {
      d[i][j] = INT_MAX;
      vis[i][j] = false;
      if (s[i][j] == 'S') {
        is = i;
        js = j;
      }
      if (s[i][j] == 'E') {
        ie = i;
        je = j;
      }
    }
  long long int delta[4][2];
  delta[0][0] = -1;
  delta[0][1] = 0;
  delta[1][0] = 0;
  delta[1][1] = -1;
  delta[2][0] = 1;
  delta[2][1] = 0;
  delta[3][0] = 0;
  delta[3][1] = 1;
  vis[ie][je] = true;
  d[ie][je] = 0;
  q.push(make_pair(ie, je));
  while (!q.empty()) {
    i = q.front().first;
    j = q.front().second;
    q.pop();
    for (long long int k = 0; k < 4; k++)
      if (i + delta[k][0] >= 0 and i + delta[k][0] < r and
          j + delta[k][1] >= 0 and j + delta[k][1] < c) {
        if (vis[i + delta[k][0]][j + delta[k][1]] == false and
            s[i + delta[k][0]][j + delta[k][1]] != 'T') {
          d[i + delta[k][0]][j + delta[k][1]] = d[i][j] + 1;
          vis[i + delta[k][0]][j + delta[k][1]] = true;
          q.push(make_pair(i + delta[k][0], j + delta[k][1]));
        }
      }
  }
  long long int m = d[is][js], ans = 0;
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++) {
      if (s[i][j] >= '0' and s[i][j] <= '9' and d[i][j] <= m) {
        ans += s[i][j] - '0';
      }
    }
  cout << ans << endl;
}
