#include <bits/stdc++.h>
using namespace std;
int dd[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<int>> mat(1000, vector<int>(1000));
int n, m, si, sj;
void bfs() {
  mat[si][sj] = 1;
  queue<pair<int, int>> q;
  q.push({si, sj});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ti = p.first + dd[i][0];
      int tj = p.second + dd[i][1];
      if (ti >= 0 && tj >= 0 && ti < n && tj < m && !mat[ti][tj]) {
        mat[ti][tj] = mat[p.first][p.second] + 1;
        q.push({ti, tj});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  int ei, ej;
  vector<pair<int, pair<int, int>>> v;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'E') {
        si = i, sj = j;
        mat[i][j] = 0;
      } else {
        if (s[j] == 'T')
          mat[i][j] = -1;
        else {
          mat[i][j] = 0;
          if (s[j] == 'S')
            ei = i, ej = j;
          else
            v.push_back({s[j] - '0', {i, j}});
        }
      }
    }
  }
  bfs();
  int ans = 0;
  int my = mat[ei][ej];
  for (auto i : v) {
    int cur = mat[i.second.first][i.second.second];
    if (cur && cur <= my) {
      ans += i.first;
    }
  }
  cout << ans;
}
