#include <bits/stdc++.h>
using namespace std;
int n, m;
int sx, sy, ex, ey;
char M[2010][2010];
int D[2010][2010];
queue<int> Q;
bool AK(int y, int x) {
  if (y >= n or y < 0 or x >= m or x < 0) return false;
  return true;
}
void Proc(int y, int x, int y_, int x_) {
  if (AK(y, x) and D[y][x] == -1 and M[y][x] != 'T') {
    D[y][x] = D[y_][x_] + 1;
    Q.push(y);
    Q.push(x);
  }
}
void BFS(int y, int x) {
  Q.push(y);
  Q.push(x);
  D[y][x] = 0;
  while (!Q.empty()) {
    y = Q.front();
    Q.pop();
    x = Q.front();
    Q.pop();
    Proc(y + 1, x, y, x);
    Proc(y, x + 1, y, x);
    Proc(y - 1, x, y, x);
    Proc(y, x - 1, y, x);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> M[i];
    for (int j = 0; j < m; j++)
      if (M[i][j] == 'S') {
        sx = j;
        sy = i;
      } else if (M[i][j] == 'E') {
        ex = j;
        ey = i;
      }
  }
  memset(D, -1, sizeof D);
  BFS(ey, ex);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (M[i][j] >= '0' and M[i][j] <= '9' and D[i][j] > -1) {
        if (D[i][j] <= D[sy][sx]) ans += M[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
  return 0;
}
