#include <bits/stdc++.h>
using namespace std;
char a[1001][1001];
int n, m;
int ans[1001][1001];
int vis[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int hehe[1001][1001];
struct node {
  int v;
  int x;
  int y;
};
int isout(int x, int y) { return (x < 0 || x >= n || y < 0 || y >= m); }
int main() {
  memset(ans, -1, sizeof(ans));
  memset(hehe, 0, sizeof(hehe));
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", &a[i]);
  queue<node> Q;
  node e, s;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'E') {
        e.v = 0;
        e.x = i;
        e.y = j;
      }
      if (a[i][j] == 'S') {
        s.x = i;
        s.y = j;
      }
    }
  Q.push(e);
  hehe[e.x][e.y] = 1;
  while (!Q.empty()) {
    node tmp = Q.front();
    Q.pop();
    int cntx = tmp.x, cnty = tmp.y, cntv = tmp.v;
    for (int i = 0; i < 4; i++) {
      int tx = cntx + vis[i][0];
      int ty = cnty + vis[i][1];
      if (!hehe[tx][ty] && !isout(tx, ty) && a[tx][ty] != 'T') {
        hehe[tx][ty] = 1;
        tmp.v = cntv + 1;
        tmp.x = tx;
        tmp.y = ty;
        Q.push(tmp);
        ans[tx][ty] = tmp.v;
      }
    }
  }
  int cnt = ans[s.x][s.y];
  int ret = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (ans[i][j] != -1 && ans[i][j] <= cnt && a[i][j] <= '9' &&
          a[i][j] >= '0')
        ret += a[i][j] - '0';
    }
  printf("%d\n", ret);
  return 0;
}
