#include <bits/stdc++.h>
using namespace std;
char c[1002][1002];
int dst[1002][1002];
int mx, my, ex, ey;
vector<pair<int, int> > e;
int n, m;
inline void go(int x, int y) {
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair(make_pair(x, y), 0));
  pair<pair<int, int>, int> z;
  pair<int, int> t;
  int h;
  while (!q.empty()) {
    z = q.front();
    q.pop();
    t = z.first;
    h = z.second;
    if (c[t.first][t.second] == 'T') continue;
    if (dst[t.first][t.second] > -1) continue;
    dst[t.first][t.second] = h;
    x = t.first;
    y = t.second;
    if (y < m) q.push(make_pair(make_pair(x, y + 1), h + 1));
    if (x < n) q.push(make_pair(make_pair(x + 1, y), h + 1));
    if (x > 0) q.push(make_pair(make_pair(x - 1, y), h + 1));
    if (y > 0) q.push(make_pair(make_pair(x, y - 1), h + 1));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'E') {
        ex = i;
        ey = j;
      }
      if (c[i][j] == 'S') {
        mx = i;
        my = j;
      }
      if (isdigit(c[i][j]) and c[i][j] > '0') {
        e.push_back(make_pair(i, j));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dst[i][j] = -1;
    }
  }
  go(ex, ey);
  int cnt = 0;
  for (int i = 0; i < e.size(); i++) {
    if (dst[e[i].first][e[i].second] != -1 &&
        dst[e[i].first][e[i].second] <= dst[mx][my]) {
      cnt += (c[e[i].first][e[i].second] - '0');
    }
  }
  cout << cnt << endl;
  return 0;
}
