#include <bits/stdc++.h>
using namespace std;
const int ux[] = {1, -1, 0, 0};
const int uy[] = {0, 0, 1, -1};
char c[1010][1010];
bool was[1010][1010];
int n, m, i, j, X, Y, x2, y2, d[1010][1010], qx[1010 * 1010], qy[1010 * 1010];
int bfs(int x, int y) {
  qx[0] = x, qy[0] = y, d[x][y] = 0, was[x][y] = true;
  i = 0, j = 1;
  for (i = 0; i < j; ++i) {
    for (int k = 0; k < 4; ++k) {
      int xx = qx[i] + ux[k];
      int yy = qy[i] + uy[k];
      if (0 < xx && xx <= n && 0 < yy && yy <= m && c[xx][yy] != 'T' &&
          !was[xx][yy]) {
        qx[j] = xx;
        qy[j] = yy;
        was[xx][yy] = true;
        d[xx][yy] = d[qx[i]][qy[i]] + 1;
        ++j;
      }
    }
  }
  int ans = 0;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if ('0' <= c[i][j] && c[i][j] <= '9' && was[i][j] == true &&
          d[i][j] <= d[X][Y])
        ans += int(c[i][j]) - 48;
  return ans;
}
int main() {
  scanf("%d%d\n", &n, &m);
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= m; ++j) {
      scanf("%c", &c[i][j]);
      if (c[i][j] == 'S') X = i, Y = j;
      if (c[i][j] == 'E') x2 = i, y2 = j;
    }
    scanf("\n");
  }
  cout << bfs(x2, y2);
  return 0;
}
