#include <bits/stdc++.h>
using namespace std;
char ar[1003][1003];
long long x, y, x2, y2;
long long vis[1003][1003];
void bfs(int i, int j) {
  vis[i][j] = 1;
  deque<int> d, q;
  int w, e;
  d.push_back(i);
  q.push_back(j);
  while (!d.empty() && !q.empty()) {
    w = d.front();
    e = q.front();
    d.pop_front();
    q.pop_front();
    if (ar[w + 1][e] != 'T') {
      if (vis[w + 1][e] == 0) {
        vis[w + 1][e] = vis[w][e] + 1;
        d.push_back(w + 1);
        q.push_back(e);
      }
    }
    if (ar[w - 1][e] != 'T') {
      if (vis[w - 1][e] == 0) {
        vis[w - 1][e] = vis[w][e] + 1;
        d.push_back(w - 1);
        q.push_back(e);
      }
    }
    if (ar[w][e + 1] != 'T') {
      if (vis[w][e + 1] == 0) {
        vis[w][e + 1] = vis[w][e] + 1;
        d.push_back(w);
        q.push_back(e + 1);
      }
    }
    if (ar[w][e - 1] != 'T') {
      if (vis[w][e - 1] == 0) {
        vis[w][e - 1] = vis[w][e] + 1;
        d.push_back(w);
        q.push_back(e - 1);
      }
    }
  }
}
int main() {
  int a, b;
  cin >> a >> b;
  vector<pair<int, int> > v;
  for (int i = 0; i <= a + 1; i++) {
    for (int j = 0; j <= b + 1; j++) {
      if (i == 0 || i == a + 1 || j == 0 || j == b + 1)
        ar[i][j] = 'T';
      else {
        cin >> ar[i][j];
        if (ar[i][j] == 'E') {
          x = i;
          y = j;
        }
        if (ar[i][j] == 'S') {
          x2 = i;
          y2 = j;
        }
        if (isdigit(ar[i][j]) && ar[i][j] != '0') {
          v.push_back(make_pair(i, j));
        }
      }
    }
  }
  bfs(x, y);
  int ans = 0, p;
  for (int i = 0; i < v.size(); i++) {
    if (vis[v[i].first][v[i].second] <= vis[x2][y2] &&
        vis[v[i].first][v[i].second] != 0) {
      p = ar[v[i].first][v[i].second] - '0';
      ans += p;
    }
  }
  cout << ans << endl;
}
