#include <bits/stdc++.h>
using namespace std;
char a[1200][1200];
int n, m, Ex, Ey, Sx, Sy, f[1200][1200], x, y, b[1200][1200],
    cou = 0, cx[1200 * 1200], cy[1200 * 1200], p, result = 0, d1, r, r1;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
queue<int> q;
bool freepoint[1200][1200];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (0), _b = (n); i <= _b; i++)
    for (int j = (0), _b = (m); j <= _b; j++) {
      f[i][j] = 0;
      freepoint[i][j] = true;
    }
  for (int i = (0), _b = (n - 1); i <= _b; i++) scanf("%s", a[i]);
  for (int i = (0), _b = (n - 1); i <= _b; i++)
    for (int j = (0), _b = (m - 1); j <= _b; j++)
      if (a[i][j] != 'T') {
        cou++;
        b[i][j] = cou;
        cx[cou] = i;
        cy[cou] = j;
        if (a[i][j] == 'E') {
          Ex = i;
          Ey = j;
        }
        if (a[i][j] == 'S') {
          Sx = i;
          Sy = j;
        }
      }
  p = b[Ex][Ey];
  q.push(p);
  freepoint[Ex][Ey] = false;
  while (q.size() > 0) {
    r = q.front();
    q.pop();
    x = cx[r];
    y = cy[r];
    for (int ix = (0), _b = (3); ix <= _b; ix++) {
      int ddx = x + dx[ix], ddy = y + dy[ix];
      if ((ddx <= n - 1) && (ddx >= 0) && (ddy >= 0) && (ddy <= m - 1))
        if (freepoint[ddx][ddy])
          if (a[ddx][ddy] != 'T') {
            freepoint[ddx][ddy] = false;
            f[ddx][ddy] = f[x][y] + 1;
            r1 = b[ddx][ddy];
            q.push(r1);
          }
    }
  }
  d1 = f[Sx][Sy];
  for (int i = (0), _b = (n - 1); i <= _b; i++)
    for (int j = (0), _b = (m - 1); j <= _b; j++)
      if (a[i][j] != 'T')
        if ((i != Ex) || (j != Ey))
          if ((i != Sx) || (j != Sy))
            if (freepoint[i][j] == false)
              if (f[i][j] <= d1) {
                result += int(a[i][j]) - int('0');
              }
  cout << result << endl;
  return 0;
}
