#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 1010;
char mat[MAXN][MAXN];
int dist[MAXN][MAXN];
int n, m;
int dc[] = {1, 0, -1, 0};
int dr[] = {0, 1, 0, -1};
void bfs(int r, int c) {
  memset(dist, -1, sizeof(dist));
  dist[r][c] = 0;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(r, c));
  while (!q.empty()) {
    int rr = q.front().first, cc = q.front().second;
    q.pop();
    for (int a = 0; a < 4; ++a) {
      int nr = rr + dr[a], nc = cc + dc[a];
      if (nr >= n || nc >= m || nr < 0 || nc < 0) continue;
      if (dist[nr][nc] != -1) continue;
      if (mat[nr][nc] == 'T') continue;
      dist[nr][nc] = dist[rr][cc] + 1;
      q.push(pair<int, int>(nr, nc));
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int er, ec;
  int sr, sc;
  for (int a = 0; a < n; ++a) {
    for (int b = 0; b < m; ++b) {
      scanf(" %c", &mat[a][b]);
      if (mat[a][b] == 'E') {
        er = a;
        ec = b;
      }
      if (mat[a][b] == 'S') {
        sr = a;
        sc = b;
      }
    }
  }
  bfs(er, ec);
  int ans = 0;
  for (int a = 0; a < n; ++a)
    for (int b = 0; b < m; ++b)
      if ('1' <= mat[a][b] && mat[a][b] <= '9' && dist[a][b] <= dist[sr][sc] &&
          dist[a][b] != -1)
        ans += mat[a][b] - '0';
  printf("%d\n", ans);
  return 0;
}
