#include <bits/stdc++.h>
using namespace std;
int dx[] = {+0, +1, +0, -1};
int dy[] = {+1, +0, -1, +0};
int A[1010][1010], vis[1010][1010], dis[1010][1010], r, C;
queue<pair<int, int> > q;
pair<int, int> src, dest, nw, tmp;
bool check(pair<int, int> tmp) {
  if (tmp.first < 1 || tmp.first > r || tmp.second < 1 || tmp.second > C ||
      vis[tmp.first][tmp.second] || A[tmp.first][tmp.second] == -1)
    return 1;
  return 0;
}
void BFS() {
  q.push(dest);
  dis[dest.first][dest.second] = 0;
  while (!q.empty()) {
    nw = q.front();
    q.pop();
    for (int i = 0; i <= 3; i++) {
      tmp = nw;
      tmp.first += dx[i];
      tmp.second += dy[i];
      if (check(tmp)) continue;
      if (!vis[tmp.first][tmp.second]) {
        vis[tmp.first][tmp.second] = 1;
        dis[tmp.first][tmp.second] = dis[nw.first][nw.second] + 1;
        q.push(tmp);
      }
    }
  }
}
int main() {
  int i, n, j, k, cs, t;
  scanf("%d %d", &r, &C);
  getchar();
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= C; j++) {
      dis[i][j] = (1 << 29);
      char c = getchar();
      if (c == 'E')
        dest = {i, j};
      else if (c == 'S')
        src = {i, j};
      else if (c == 'T')
        A[i][j] = -1;
      else
        A[i][j] = c - '0';
    }
    getchar();
  }
  BFS();
  int dist = dis[src.first][src.second];
  int ans = 0;
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= C; j++) {
      if (dist >= dis[i][j] && A[i][j] != -1) {
        ans += A[i][j];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
