#include <bits/stdc++.h>
using namespace std;
char G[1005][1005];
int dis[1005 * 1005], S;
const int INF = 0x3f3f3f3f;
vector<int> V, N;
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
queue<int> Q;
int r, c;
void SPFA() {
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    int x = v / c, y = v % c;
    for (int k = 0; k < 4; ++k) {
      int nx = x + dir[k][0], ny = y + dir[k][1];
      int nv = nx * c + ny;
      if (0 <= nx && nx < r && 0 <= ny && ny < c && G[nx][ny] != 'T' &&
          dis[nv] == INF) {
        dis[nv] = dis[v] + 1;
        Q.push(nv);
      }
    }
  }
}
int main() {
  memset(dis, INF, sizeof(dis));
  scanf("%d%d", &r, &c);
  for (int i = 0; i < r; ++i) {
    scanf("%s", G[i]);
    for (int j = 0; j < c; ++j) {
      int v = i * c + j;
      if (G[i][j] == 'E')
        Q.push(v), dis[v] = 0;
      else if (G[i][j] == 'S')
        S = v;
      else if ('0' < G[i][j] && G[i][j] <= '9')
        V.push_back(v), N.push_back(G[i][j] - '0');
    }
  }
  SPFA();
  int d = dis[S], ans = 0;
  for (int i = 0; i < V.size(); ++i) {
    int v = V[i];
    if (dis[v] <= d) ans += N[i];
  }
  printf("%d\n", ans);
  return 0;
}
