#include <bits/stdc++.h>
using namespace std;
using namespace std;
queue<pair<int, int> > q;
const int N = 1005;
int n, m;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
char a[N][N];
int len[N][N];
int x, y;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", a[i]);
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      len[i][j] = 1e9;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == 'S') {
        x = i;
        y = j;
      } else if (a[i][j] == 'E') {
        len[i][j] = 0;
        q.push(make_pair(i, j));
      }
    }
  }
  while (!q.empty()) {
    int cx = q.front().first;
    int cy = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = cx + dx[i];
      int ny = cy + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
      if (a[nx][ny] == 'T') continue;
      if (len[nx][ny] > len[cx][cy] + 1) {
        len[nx][ny] = len[cx][cy] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  int time = len[x][y];
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] >= '0' && a[i][j] <= '9') {
        if (len[i][j] <= time) ans += a[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
  return 0;
}
