#include <bits/stdc++.h>
using namespace std;
int n, m, sx, sy, ma[1005][1005], vis[1005][1005], ans, t, tot = 99999999;
int dx[5] = {1, -1, 0, 0};
int dy[5] = {0, 0, 1, -1};
char maa[1005][1005];
void bfs(int u, int v) {
  queue<int> qx;
  queue<int> qy;
  queue<int> qt;
  qx.push(u), qy.push(v), qt.push(0);
  vis[u][v] = 1;
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    int t = qt.front();
    qt.pop();
    if (t > tot) continue;
    if (ma[x][y] == -2) {
      tot = t;
    }
    if (ma[x][y] > 0) ans += ma[x][y];
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!vis[xx][yy] && ma[xx][yy] != -1 && xx <= n && xx > 0 && yy <= m &&
          yy > 0) {
        vis[xx][yy] = 1;
        qx.push(xx), qy.push(yy), qt.push(t + 1);
      }
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", maa[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (maa[i][j] == 'T')
        ma[i][j] = -1;
      else if (maa[i][j] == 'S')
        ma[i][j] = -2;
      else if (maa[i][j] == 'E') {
        ma[i][j] = 0;
        sx = i, sy = j;
      } else
        ma[i][j] = maa[i][j] - '0';
    }
  }
  bfs(sx, sy);
  cout << ans << endl;
  return 0;
}
