#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
struct Node {
  int x, y, step;
};
int s[1005][1005];
char mp[1005][1005];
bool vis[1005][1005];
int dir[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int n, m, sx, sy, ex, ey, ans, shortest;
bool ISok(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && mp[x][y] != 'T')
    return true;
  return false;
}
void bfs() {
  int flag = 0;
  queue<Node> Q;
  memset(vis, false, sizeof(vis));
  Node st, now;
  while (!Q.empty()) Q.pop();
  st.x = sx, st.y = sy, st.step = 0;
  vis[sx][sy] = true;
  Q.push(st);
  while (!Q.empty()) {
    st = Q.front();
    Q.pop();
    if (st.x == ex && st.y == ey) {
      shortest = st.step;
      return;
    }
    for (int i = 0; i < 4; i++) {
      int dx = st.x + dir[i][0];
      int dy = st.y + dir[i][1];
      if (ISok(dx, dy)) {
        if (isdigit(mp[dx][dy]) && mp[dx][dy] != '0') s[dx][dy] = st.step + 1;
        now.x = dx;
        now.y = dy;
        now.step = st.step + 1;
        vis[dx][dy] = true;
        Q.push(now);
      }
    }
  }
  return;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    ans = 0;
    memset(s, 0, sizeof(s));
    for (int i = 0; i < n; i++) {
      scanf("%s", mp[i]);
      for (int j = 0; j < m; j++) {
        if (mp[i][j] == 'E') sx = i, sy = j;
        if (mp[i][j] == 'S') ex = i, ey = j;
      }
    }
    bfs();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (s[i][j] && s[i][j] <= shortest) ans += (mp[i][j] - '0');
    printf("%d\n", ans);
  }
  return 0;
}
