#include <bits/stdc++.h>
using namespace std;
char s[1003][1003];
int a[1003][1003];
bool flag[1003][1003];
int n, m;
int movex[10], movey[10];
int xod;
struct ocher {
  int y, x;
};
ocher que[1000003];
int head, tail;
int ans;
void bfs() {
  int i;
  movex[0] = 1;
  movey[0] = 0;
  movex[1] = 0;
  movey[1] = 1;
  movex[2] = -1;
  movey[2] = 0;
  movex[3] = 0;
  movey[3] = -1;
  int x, y;
  while (head > tail) {
    x = que[tail].x;
    y = que[tail].y;
    if (s[y][x] == 'S') xod = a[y][x];
    for (i = 0; i < 4; i++) {
      if (!flag[y + movey[i]][x + movex[i]] &&
          s[y + movey[i]][x + movex[i]] != 'T') {
        if (y + movey[i] < n && y + movey[i] > -1 && x + movex[i] < m &&
            x + movex[i] > -1) {
          flag[y + movey[i]][x + movex[i]] = true;
          que[head].x = x + movex[i];
          que[head].y = y + movey[i];
          a[y + movey[i]][x + movex[i]] = a[y][x] + 1;
          head++;
        }
      }
    }
    tail++;
  }
  return;
}
int main() {
  scanf("%d %d", &n, &m);
  int i, j;
  for (i = 0; i < n; i++) scanf("%s", s[i]);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      if (s[i][j] == 'E') {
        que[0].y = i;
        que[0].x = j;
        a[i][j] = 0;
        flag[i][j] = true;
      }
  head = 1;
  tail = 0;
  bfs();
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      if (a[i][j] <= xod) {
        if (s[i][j] != 'T' && s[i][j] != 'E' && s[i][j] != 'S' && flag[i][j]) {
          ans += s[i][j] - '0';
        }
      }
    }
  printf("%d\n", ans);
  return 0;
}
