#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:650000000")
using namespace std;
template <typename T>
T mabs(const T &a) {
  return a < 0 ? -a : a;
}
char A[1010][1010];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int D[1010][1010];
void bfs(int x, int y, int n, int m) {
  queue<pair<int, int> > q;
  q.push(pair<int, int>(x, y));
  memset(D, -1, sizeof(D));
  D[x][y] = 0;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    for (int k = (0), ek = (4); k < ek; k++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m && D[nx][ny] == -1 &&
          A[nx][ny] != 'T') {
        D[nx][ny] = D[x][y] + 1;
        q.push(pair<int, int>(nx, ny));
      }
    }
  }
}
void run() {
  int n, m;
  cin >> n >> m;
  for (int i = (0), ei = (n); i < ei; i++) scanf("%s", A[i]);
  int ex, ey;
  int sx, sy;
  for (int i = (0), ei = (n); i < ei; i++)
    for (int j = (0), ej = (m); j < ej; j++)
      if (A[i][j] == 'E') {
        ex = i;
        ey = j;
      } else if (A[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  bfs(ex, ey, n, m);
  int res = 0;
  for (int i = (0), ei = (n); i < ei; i++)
    for (int j = (0), ej = (m); j < ej; j++)
      if (isdigit(A[i][j]) && D[i][j] <= D[sx][sy] && D[i][j] != -1)
        res += A[i][j] - '0';
  cout << res << endl;
}
int main() {
  run();
  return 0;
}
