#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, m, ans, d[1005][1005];
char a[1005][1005];
bool vis[1005][1005];
queue<int> q;
int main() {
  int i, j, x, y, xe, ye, xs, ys, nx, ny;
  memset(vis, false, sizeof(vis));
  memset(d, -1, sizeof(d));
  scanf("%d%d", &n, &m);
  for (i = 0; i < (n); i++) {
    scanf("%s", a[i]);
    for (j = 0; j < (m); j++) {
      if (a[i][j] == 'E') {
        xe = i;
        ye = j;
      }
      if (a[i][j] == 'S') {
        xs = i;
        ys = j;
      }
    }
  }
  vis[xe][ye] = true;
  d[xe][ye] = 0;
  q.push(xe);
  q.push(ye);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    y = q.front();
    q.pop();
    for (i = 0; i < (4); i++) {
      nx = x + dx[i];
      ny = y + dy[i];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] != 'T' &&
          !vis[nx][ny]) {
        q.push(nx);
        q.push(ny);
        vis[nx][ny] = true;
        d[nx][ny] = d[x][y] + 1;
      }
    }
  }
  ans = 0;
  for (i = 0; i < (n); i++) {
    for (j = 0; j < (m); j++) {
      if (a[i][j] >= '0' && a[i][j] <= '9' && d[i][j] != -1 &&
          d[i][j] <= d[xs][ys])
        ans += a[i][j] - '0';
    }
  }
  printf("%d\n", ans);
  return 0;
}
