#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 2e18 + 5;
vector<long long> adj[1000001];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long r, c;
  cin >> r >> c;
  vector<string> a(r);
  for (long long i = (0); i < (r); i++) cin >> a[i];
  long long cnt = 0;
  unordered_map<long long, long long> mikemon;
  long long dest, sourc, x;
  for (long long i = (0); i < (r); i++) {
    for (long long j = (0); j < (c); j++) {
      ++cnt;
      if (a[i][j] == 'T') continue;
      if (i != 0) {
        if (a[i - 1][j] != 'T') {
          adj[cnt].push_back(cnt - c);
          adj[cnt - c].push_back(cnt);
        }
      }
      if (i != r - 1) {
        if (a[i + 1][j] != 'T') {
          adj[cnt].push_back(cnt + c);
          adj[cnt + c].push_back(cnt);
        }
      }
      if (j != 0) {
        if (a[i][j - 1] != 'T') {
          adj[cnt].push_back(cnt - 1);
          adj[cnt - 1].push_back(cnt);
        }
      }
      if (j != c - 1) {
        if (a[i][j + 1] != 'T') {
          adj[cnt].push_back(cnt + 1);
          adj[cnt + 1].push_back(cnt);
        }
      }
      if (a[i][j] == 'E')
        dest = cnt;
      else if (a[i][j] == 'S')
        sourc = cnt;
      else {
        mikemon.insert({cnt, a[i][j] - '0'});
      }
    }
  }
  queue<long long> q;
  vector<long long> vis(cnt + 1, 0), d(cnt + 1, inf);
  vis[dest] = 1;
  d[dest] = 1;
  q.push(dest);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (auto v : adj[x]) {
      if (vis[v] == 0) {
        vis[v] = 1;
        q.push(v);
        d[v] = d[x] + 1;
      }
    }
  }
  long long ans = 0;
  long long y = d[sourc];
  for (long long i = (1); i < (cnt + 1); i++) {
    if (d[i] <= y) ans += mikemon[i];
  }
  cout << ans << endl;
  return 0;
}
