#include <bits/stdc++.h>
using namespace std;
int Get() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && (c != '-'))
    ;
  bool Flag = (c == '-');
  if (Flag) c = getchar();
  int X = 0;
  while (c >= '0' && c <= '9') {
    X = X * 10 + c - 48;
    c = getchar();
  }
  return Flag ? -X : X;
}
void Output(int X) {
  if (X < 0) {
    putchar('-');
    X = -X;
  }
  int Len = 0, Data[10];
  while (X) {
    Data[Len++] = X % 10;
    X /= 10;
  }
  if (!Len) Data[Len++] = 0;
  while (Len--) putchar(Data[Len] + 48);
  putchar('\n');
}
const int V[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int main() {
  int M = Get(), N = Get(), Sx, Sy, Tx, Ty;
  static char Map[1000][1000];
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++) {
      char c;
      while (c = getchar(),
             (c < '0' || c > '9') && c != 'S' && c != 'T' && c != 'E')
        ;
      Map[i][j] = c;
      if (c == 'S') {
        Sx = i;
        Sy = j;
      }
      if (c == 'E') {
        Tx = i;
        Ty = j;
      }
    }
  static int Qx[1000000], Qy[1000000];
  Qx[0] = Tx;
  Qy[0] = Ty;
  int Front = 0, Rear = 1;
  static bool Flag[1000][1000];
  static int Dist[1000][1000];
  memset(Flag, 0, sizeof(Flag));
  Dist[Tx][Ty] = 0;
  Flag[Tx][Ty] = true;
  while (Front < Rear) {
    int X0 = Qx[Front], Y0 = Qy[Front];
    Front++;
    for (int D = 0; D < 4; D++) {
      int X1 = X0 + V[D][0], Y1 = Y0 + V[D][1];
      if (X1 >= 0 && X1 < M && Y1 >= 0 && Y1 < N && Map[X1][Y1] != 'T' &&
          !Flag[X1][Y1]) {
        Flag[X1][Y1] = true;
        Dist[X1][Y1] = Dist[X0][Y0] + 1;
        Qx[Rear] = X1;
        Qy[Rear] = Y1;
        Rear++;
      }
    }
  }
  int Ans = 0;
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
      if (Map[i][j] >= '1' && Map[i][j] <= '9' && Flag[i][j])
        if (Dist[i][j] <= Dist[Sx][Sy]) Ans += Map[i][j] - 48;
  Output(Ans);
  return 0;
}
