#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
};
struct node2 {
  int x, y, w;
  bool friend operator<(node2 a, node2 b) { return a.w > b.w; }
};
int r, c, dist[1010][1010];
char num[1010][1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool flag[1010][1010];
priority_queue<node2> q;
void Init() {
  scanf("%d%d\n", &r, &c);
  for (int i = 0; i < r; i++) scanf("%s\n", num[i]);
}
void dijkstra(node uu) {
  node2 u, p;
  memset(dist, 127, sizeof(dist));
  u.x = uu.x;
  u.y = uu.y;
  u.w = 0;
  q.push(u);
  dist[u.x][u.y] = 0;
  while (!q.empty()) {
    u = q.top();
    q.pop();
    if (flag[u.x][u.y]) continue;
    flag[u.x][u.y] = 1;
    for (int i = 0; i < 4; i++) {
      int tx = u.x + dx[i], ty = u.y + dy[i];
      if (tx >= 0 && tx < r && ty >= 0 && ty < c && num[tx][ty] != 'T' &&
          dist[tx][ty] > dist[u.x][u.y] + 1) {
        dist[tx][ty] = dist[u.x][u.y] + 1;
        if (!flag[tx][ty]) {
          p.x = tx;
          p.y = ty;
          p.w = dist[tx][ty];
          q.push(p);
        }
      }
    }
  }
}
void solve() {
  node u;
  for (u.x = 0; u.x < r; u.x++) {
    for (u.y = 0; u.y < c; u.y++)
      if (num[u.x][u.y] == 'E') break;
    if (u.y <= c - 1) break;
  }
  dijkstra(u);
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (num[i][j] == 'S') {
        u.x = i;
        u.y = j;
        break;
      }
  int total = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (num[i][j] > '0' && num[i][j] <= '9' && dist[i][j] <= dist[u.x][u.y])
        total += num[i][j] - '0';
  printf("%d\n", total);
}
int main() {
  Init();
  solve();
  return 0;
}
