#include <bits/stdc++.h>
using namespace std;
bool mark[1005][1005];
char a[1005][1005];
int shortest[1005][1005];
struct point {
  int x, y, t;
  point() { x = y = t = 0; }
  point(int x_, int y_, int t_) {
    x = x_;
    y = y_;
    t = t_;
  }
};
void BFS(int nowX, int nowY, int x, int y) {
  queue<point> q;
  q.push(point(nowX, nowY, 0));
  mark[nowX][nowY] = true;
  shortest[nowX][nowY] = 0;
  while (q.size()) {
    point top = q.front();
    int X = top.x;
    int Y = top.y;
    int val = top.t;
    q.pop();
    for (int ix = -1; ix <= 1; ix++)
      for (int iy = -1; iy <= 1; iy++)
        if (ix == 0 ^ iy == 0) {
          if (X + ix >= 0 && X + ix < x && Y + iy >= 0 && Y + iy < y &&
              a[X + ix][Y + iy] != 'T' && !mark[X + ix][Y + iy]) {
            mark[X + ix][Y + iy] = true;
            shortest[X + ix][Y + iy] = val + 1;
            q.push(point(X + ix, Y + iy, val + 1));
          }
        }
  }
}
vector<point> breed;
int main() {
  int y, x;
  cin >> y >> x;
  int sx, sy, ex, ey;
  for (int i = 0; i < y; i++)
    for (int j = 0; j < x; j++) {
      cin >> a[j][i];
      if (a[j][i] == 'S') {
        sx = j;
        sy = i;
      } else if (a[j][i] == 'E') {
        ex = j;
        ey = i;
      } else if (a[j][i] >= '0' && a[j][i] <= '9')
        breed.push_back(point(j, i, a[j][i] - '0'));
    }
  BFS(ex, ey, x, y);
  int ans = 0;
  for (int i = 0; i < breed.size(); i++)
    if (shortest[breed[i].x][breed[i].y] <= shortest[sx][sy] &&
        shortest[breed[i].x][breed[i].y] > 0)
      ans += breed[i].t;
  cout << ans << endl;
  return 0;
}
