#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
struct node {
  int x, y, dep;
} l;
queue<node> Q;
char g[1111][1111];
int v[1111][1111], s[1111][1111];
int n, m, i, j, k, ans;
int x, y, x_e, y_e, x_s, y_s;
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%s", g[i] + 1);
    for (j = 1; j <= m; j++) {
      if (g[i][j] == 'E') x_e = i, y_e = j;
      if (g[i][j] == 'S') x_s = i, y_s = j;
      if (g[i][j] != 'T') v[i][j] = 1;
    }
  }
  memset(s, 0x3f, sizeof s);
  Q.push(node{x_e, y_e, 0});
  s[x_e][y_e] = v[x_e][y_e] = 0;
  while (!Q.empty()) {
    l = Q.front();
    Q.pop();
    for (k = 0; k < 4; k++) {
      x = l.x + dx[k];
      y = l.y + dy[k];
      if (v[x][y]) {
        Q.push(node{x, y, l.dep + 1});
        s[x][y] = min(s[x][y], l.dep + 1);
        v[x][y] = 0;
      }
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (isdigit(g[i][j]) && s[i][j] <= s[x_s][y_s]) ans += g[i][j] - '0';
  printf("%d\n", ans);
}
