#include <bits/stdc++.h>
using namespace std;
char a[1010][1010];
bool flag[1010][1010];
int dist[1010][1010];
int n, m;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
struct S {
  int x, y, dep;
  S(int x_, int y_, int dep_) : x(x_), y(y_), dep(dep_) {}
};
void floodfill(int x, int y) {
  queue<S> q;
  q.push(S(x, y, 0));
  int dep = 0;
  memset(dist, 0x4F4F4F4F, sizeof(dist));
  dist[x][y] = 0;
  memset(flag, 0, sizeof(flag));
  while (!q.empty()) {
    int nx, ny;
    nx = q.front().x;
    ny = q.front().y;
    dep = q.front().dep;
    q.pop();
    int px, py;
    for (int k = 0; k < 4; k++) {
      px = nx + dx[k];
      py = ny + dy[k];
      if (px < 0 || px >= n || py < 0 || py >= m) continue;
      if (a[px][py] == 'T') continue;
      dist[px][py] = min(dist[px][py], dep + 1);
      if (!flag[px][py]) q.push(S(px, py, dep + 1));
      flag[px][py] = true;
    }
  }
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  int ex, ey;
  int sx, sy;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'E') {
        ex = i;
        ey = j;
      }
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  floodfill(ex, ey);
  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 (dist[i][j] <= dist[sx][sy]) ans += a[i][j] - '0';
      }
    }
  printf("%d\n", ans);
  return 0;
}
