#include <bits/stdc++.h>
inline void in(int &MAGNUS) { scanf("%d", &MAGNUS); }
inline void out(int MAGNUS) { printf("%d\n", MAGNUS); }
inline void in(int &MAGNUS, int &CLAR) { scanf("%d%d", &MAGNUS, &CLAR); }
inline void out(int MAGNUS, int CLAR) { printf("%d %d\n", MAGNUS, CLAR); }
inline void inl(long long &LIV) { scanf("%lld", &LIV); }
inline void inl(long long &LIV, long long &MART) {
  scanf("%lld%lld", &LIV, &MART);
}
inline void outl(long long LIV) { printf("%lld\n", LIV); }
inline void outl(long long LIV, long long MART) {
  printf("%lld %lld\n", LIV, MART);
}
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int R, C, sx, sy, ey, ex;
char x[1005][1005];
vector<pair<int, int> > breeder;
int dis[1005][1005];
bool inside(int r, int c) { return 1 <= r && r <= R && 1 <= c && c <= C; }
int main() {
  in(R, C);
  for (int i = 1; i <= R; i++)
    for (int j = 1; j <= C; j++) {
      cin >> x[i][j];
      if (x[i][j] == 'E')
        sx = i, sy = j;
      else if (x[i][j] == 'S')
        ex = i, ey = j;
      else if (isdigit(x[i][j])) {
        breeder.push_back(make_pair(i, j));
      }
    }
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  memset(dis, -1, sizeof(dis));
  dis[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();
    int r = now.first, c = now.second;
    for (int i = 0; i <= 3; i++) {
      int nxr = r + dx[i], nxc = c + dy[i];
      if (!inside(nxr, nxc)) continue;
      if (x[nxr][nxc] == 'T') continue;
      if (dis[nxr][nxc] != -1) continue;
      dis[nxr][nxc] = dis[r][c] + 1;
      q.push(make_pair(nxr, nxc));
    }
  }
  int ans = 0;
  for (int i = 0; i <= (int)breeder.size() - 1; i++) {
    int r = breeder[i].first, c = breeder[i].second;
    if (dis[r][c] == -1) continue;
    if (dis[r][c] <= dis[ex][ey]) ans += (x[r][c] - '0');
  }
  out(ans);
  return 0;
}
