#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, step;
} now, nex;
int n, m, Dis, cnt;
int dist[1005][1005];
char a[1005][1005];
bool vis[1005][1005];
int fx[4] = {0, 0, 1, -1};
int fy[4] = {1, -1, 0, 0};
void Bfs(int x, int y) {
  int cnt = 0;
  memset(dist, -1, sizeof(dist));
  memset(vis, 0, sizeof(vis));
  vis[x][y] = 1;
  queue<node> s;
  now.x = x;
  now.y = y;
  now.step = 0;
  s.push(now);
  while (!s.empty()) {
    now = s.front();
    s.pop();
    dist[now.x][now.y] = now.step;
    if (a[now.x][now.y] == 'S') Dis = now.step;
    for (int i = 0; i < 4; i++) {
      nex.x = now.x + fx[i];
      nex.y = now.y + fy[i];
      nex.step = now.step + 1;
      if (nex.x >= 0 && nex.x < n && nex.y >= 0 && nex.y < m &&
          vis[nex.x][nex.y] == false && a[nex.x][nex.y] != 'T') {
        vis[nex.x][nex.y] = 1;
        s.push(nex);
      }
    }
  }
  int output = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] == -1) continue;
      if (dist[i][j] <= Dis && a[i][j] >= '1' && a[i][j] <= '9') {
        output += a[i][j] - '0';
      }
    }
  }
  printf("%d\n", output);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 0; i < n; i++) scanf("%s", a[i]);
    int xx, yy;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == 'E') {
          xx = i;
          yy = j;
        }
      }
    }
    Bfs(xx, yy);
  }
}
