#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int maxm = 1e6 + 5;
int r, c;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int vis[1005][1005];
char s[1005][1005];
int dp[1005][1005];
int endx, endy;
struct node {
  int x, y, step;
  node() {}
  node(int _x, int _y, int _step) {
    x = _x;
    y = _y;
    step = _step;
  }
};
queue<node> q;
void bfs() {
  memset(dp, INF, sizeof(dp));
  q.push(node(endx, endy, 0));
  dp[endx][endy] = 0;
  while (!q.empty()) {
    node t = q.front();
    q.pop();
    if (t.x < 1 || t.y < 1 || t.x > r || t.y > c) continue;
    if (s[t.x][t.y] == 'T' || vis[t.x][t.y] == 1) continue;
    dp[t.x][t.y] = min(dp[t.x][t.y], t.step);
    vis[t.x][t.y] = 1;
    for (int i = 0; i < 4; i++) {
      int x = t.x + dx[i];
      int y = t.y + dy[i];
      q.push(node(x, y, t.step + 1));
    }
  }
}
int main() {
  cin >> r >> c;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      if (s[i][j] == 'E') {
        endx = i;
        endy = j;
      }
    }
  }
  bfs();
  int your_step;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      if (s[i][j] == 'S') {
        your_step = dp[i][j];
      }
    }
  }
  int cnt = 0;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      if (s[i][j] >= '1' && s[i][j] <= '9' && dp[i][j] <= your_step) {
        cnt += s[i][j] - '0';
      }
    }
  }
  printf("%d\n", cnt);
  return 0;
}
