#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
const double PI = acos(-1.0);
const int oo = 1 << 29;
const int N = 1100;
int n, m;
char a[N][N];
int d[N][N];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
void spfa() {
  int x, y;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'E') {
        x = i, y = j;
      }
    }
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  memset(d, -1, sizeof(d));
  d[x][y] = 0;
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    for (int r = 0; r < 4; r++) {
      int i = t.first + dx[r];
      int j = t.second + dy[r];
      if (i < 0 || j < 0 || i >= n || j >= m) continue;
      if (a[i][j] != 'T') {
        if (d[i][j] >= 0 && d[i][j] <= d[t.first][t.second] + 1) continue;
        d[i][j] = d[t.first][t.second] + 1;
        q.push(make_pair(i, j));
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  spfa();
  int my;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'S') my = d[i][j];
    }
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (isdigit(a[i][j]) && d[i][j] >= 0 && d[i][j] <= my) {
        ans += a[i][j] - '0';
      }
    }
  printf("%d\n", ans);
  return 0;
}
