#include <bits/stdc++.h>
using namespace std;
int r, c;
char mat[1010][1010];
int dist[1010][1010];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
bool canGo(int x, int y) {
  return x >= 0 && y >= 0 && x < r && y < c && mat[x][y] != 'T' &&
         dist[x][y] == -1;
}
void bfs(int x, int y, int d = 0) {
  memset(dist, -1, sizeof dist);
  queue<pair<pair<int, int>, int> > q;
  q.push(pair<pair<int, int>, int>(pair<int, int>(x, y), d));
  dist[x][y] = 0;
  int resp = -1;
  while (!q.empty()) {
    x = q.front().first.first, y = q.front().first.second, d = q.front().second;
    q.pop();
    if (mat[x][y] == 'S') resp = d;
    for (int i = (int)0; i < (int)4; i++) {
      int ax = x + dx[i], ay = y + dy[i], ad = d + 1;
      if (!canGo(ax, ay)) continue;
      q.push(pair<pair<int, int>, int>(pair<int, int>(ax, ay), ad));
      dist[ax][ay] = ad;
    }
  }
  int ans = 0;
  for (int i = (int)0; i < (int)r; i++)
    for (int j = (int)0; j < (int)c; j++) {
      if (mat[i][j] >= '1' && mat[i][j] <= '9' && dist[i][j] != -1 &&
          dist[i][j] <= resp)
        ans += mat[i][j] - '0';
    }
  cout << ans << '\n';
}
int main() {
  scanf("%d %d", &r, &c);
  for (int i = (int)0; i < (int)r; i++) scanf("%s", mat[i]);
  for (int i = (int)0; i < (int)r; i++)
    for (int j = (int)0; j < (int)c; j++) {
      if (mat[i][j] == 'E') {
        bfs(i, j);
      }
    }
  return 0;
}
