#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
char a[maxn][maxn];
int vis[maxn][maxn], dis[maxn][maxn];
int n, m, sx, sy, ex, ey, res;
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
struct node {
  int x, y, t;
};
void bfs() {
  queue<node> q;
  q.push((node){ex, ey, 0});
  while (!q.empty()) {
    node qq = q.front();
    q.pop();
    if (qq.x == sx && qq.y == sy) res = qq.t;
    dis[qq.x][qq.y] = qq.t;
    for (int i = 0; i < 4; i++) {
      int nx = qq.x + dir[i][0];
      int ny = qq.y + dir[i][1];
      if (!vis[nx][ny] && nx >= 1 && nx <= n && ny >= 1 && ny <= m &&
          a[nx][ny] != 'T') {
        vis[nx][ny] = 1;
        q.push((node){nx, ny, qq.t + 1});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sx = i, sy = j;
      if (a[i][j] == 'E') ex = i, ey = j;
    }
  }
  int ans = 0;
  memset(dis, inf, sizeof(dis));
  bfs();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] >= '0' && a[i][j] <= '9' && dis[i][j] <= res) {
        ans += a[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
  return 0;
}
