#include <bits/stdc++.h>
using namespace std;
char c[1005][1005];
bool vis[1005][1005];
long long cur, n, m, cx, cy, si, sj, ans = 0, dis;
long long dist[1005][1005];
vector<int> x, y;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void bfs(int k, int l) {
  for (cur; cur < l; cur++) {
    for (int i = 0; i < 4; i++) {
      cx = x[cur] + dx[i];
      cy = y[cur] + dy[i];
      if (cx >= 0 && cy >= 0 && cx < n && cy < m && !vis[cx][cy]) {
        vis[cx][cy] = true;
        dist[cx][cy] = k;
        x.push_back(cx);
        y.push_back(cy);
      }
    }
  }
  if (cur < x.size()) bfs(k + 1, x.size());
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j <= m; j++) {
      if (c[i][j] == 'T') {
        vis[i][j] = true;
      } else if (c[i][j] == 'S') {
        si = i;
        sj = j;
        vis[i][j] = false;
      } else if (c[i][j] == 'E') {
        x.push_back(i);
        y.push_back(j);
        vis[i][j] = true;
      } else {
        vis[i][j] = false;
      }
      dist[i][j] = 10000000000;
    }
  }
  dist[x[0]][y[0]] = 0;
  ans = 0;
  cur = 0;
  bfs(1, 1);
  dis = dist[si][sj];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == 'T' || c[i][j] == 'S' || c[i][j] == 'E') {
        continue;
      } else if (dist[i][j] <= dis) {
        ans = ans + c[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
}
