#include <bits/stdc++.h>
using namespace std;
int n, m, tx, ty, sx, sy;
char c[(1000 + 5)][(1000 + 5)];
bool b[(1000 + 5)][(1000 + 5)] = {0};
int qx[(1000 + 5) * (1000 + 5)], qy[(1000 + 5) * (1000 + 5)],
    head = 1, tail = 1, d[(1000 + 5)][(1000 + 5)];
void bfs() {
  qx[1] = tx, qy[1] = ty;
  b[tx][ty] = 1;
  memset(d, 127, sizeof(d));
  d[tx][ty] = 0;
  while (head <= tail) {
    int x = qx[head], y = qy[head];
    if (1 < x && !b[x - 1][y])
      b[x - 1][y] = 1, d[x - 1][y] = d[x][y] + 1, qx[++tail] = x - 1,
            qy[tail] = y;
    if (x < n && !b[x + 1][y])
      b[x + 1][y] = 1, d[x + 1][y] = d[x][y] + 1, qx[++tail] = x + 1,
            qy[tail] = y;
    if (1 < y && !b[x][y - 1])
      b[x][y - 1] = 1, d[x][y - 1] = d[x][y] + 1, qx[++tail] = x,
               qy[tail] = y - 1;
    if (y < m && !b[x][y + 1])
      b[x][y + 1] = 1, d[x][y + 1] = d[x][y] + 1, qx[++tail] = x,
               qy[tail] = y + 1;
    head++;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == 'S')
        sx = i, sy = j;
      else if (c[i][j] == 'E')
        tx = i, ty = j;
      else if (c[i][j] == 'T')
        b[i][j] = 1;
  bfs();
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (d[i][j] <= d[sx][sy] && isdigit(c[i][j])) ans += c[i][j] - 48;
  cout << ans << endl;
  return 0;
}
