#include <bits/stdc++.h>
const int N = (int)1005;
const int BLOCKED = -1;
const int K = 4;
const int X[] = {1, -1, 0, 0};
const int Y[] = {0, 0, 1, -1};
using namespace std;
int a[N][N], n, m, xs, ys, xf, yf, d[N][N];
queue<pair<int, int> > q;
bool good(int x, int y) {
  return x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != BLOCKED;
}
void bfs() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) d[i][j] = -1;
  q.push({xf, yf});
  d[xf][yf] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < K; i++) {
      int tx = x + X[i], ty = y + Y[i];
      if (good(tx, ty) && d[tx][ty] == -1) {
        d[tx][ty] = d[x][y] + 1;
        q.push({tx, ty});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++) {
      if (s[j - 1] == 'E') xf = i, yf = j;
      if (s[j - 1] == 'S') xs = i, ys = j;
      if (s[j - 1] == 'T') a[i][j] = BLOCKED;
      if (s[j - 1] >= '0' && s[j - 1] <= '9') a[i][j] = (s[j - 1] - '0');
    }
  }
  bfs();
  int l = d[xs][ys], ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (d[i][j] != -1 && d[i][j] <= l) ans += a[i][j];
    }
  }
  cout << ans << endl;
  return 0;
}
