#include <bits/stdc++.h>
using namespace std;
const int Size = 1024;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 2e6 + 15;
struct node {
  int x, y;
  node(int a, int b) { x = a, y = b; }
};
int n, m;
char a[1015][1015];
int d[1015][1015];
bool valid(int x, int y) {
  return x >= 1 && y >= 1 && x <= n && y <= m && a[x][y] != 'T' &&
         d[x][y] == -1;
}
int main() {
  int ox, oy, fx, fy;
  scanf("%d%d", &n, &m);
  for (int i = 1; n >= i; i++) {
    scanf("%s", a[i] + 1);
    for (int j = 1; m >= j; j++) {
      if (a[i][j] == 'S') ox = i, oy = j;
      if (a[i][j] == 'E') fx = i, fy = j;
    }
  }
  memset(d, -1, sizeof(d));
  queue<node> q;
  q.push(node(fx, fy));
  d[fx][fy] = 0;
  while (q.size()) {
    node u = q.front();
    int x = u.x, y = u.y;
    q.pop();
    if (valid(x + 1, y)) d[x + 1][y] = d[x][y] + 1, q.push(node(x + 1, y));
    if (valid(x - 1, y)) d[x - 1][y] = d[x][y] + 1, q.push(node(x - 1, y));
    if (valid(x, y - 1)) d[x][y - 1] = d[x][y] + 1, q.push(node(x, y - 1));
    if (valid(x, y + 1)) d[x][y + 1] = d[x][y] + 1, q.push(node(x, y + 1));
  }
  int ans = 0;
  int mx = d[ox][oy];
  for (int i = 1; n >= i; i++) {
    for (int j = 1; m >= j; j++) {
      if (d[i][j] <= mx && a[i][j] <= '9' && a[i][j] > '0' && d[i][j] != -1) {
        ans += a[i][j] - '0';
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
