#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, m, ans;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int dist[N][N];
char ar[N][N];
bool inRange(int x, int y) {
  return ((x >= 1) && (x <= n) && (y >= 1) && (y <= m));
}
int main() {
  cin >> n >> m;
  pair<int, int> s, me;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ar[i][j];
      if (ar[i][j] == 'E') s = {i, j};
      if (ar[i][j] == 'S') me = {i, j};
    }
  }
  memset(dist, -1, sizeof(dist));
  dist[s.first][s.second] = 0;
  queue<pair<int, int> > q;
  q.push(s);
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();
    int r = now.first;
    int c = now.second;
    for (int i = 0; i < 4; i++) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (inRange(nr, nc) && (ar[nr][nc] != 'T')) {
        if (dist[nr][nc] == -1) {
          dist[nr][nc] = dist[r][c] + 1;
          q.push(pair<int, int>(nr, nc));
        }
      }
    }
  }
  int myDist = dist[me.first][me.second];
  ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (('A' <= ar[i][j]) && (ar[i][j] <= 'Z')) continue;
      if (dist[i][j] <= myDist && (dist[i][j] != -1))
        ans += int(ar[i][j]) - int('0');
    }
  }
  cout << ans << '\n';
  return 0;
}
