#include <bits/stdc++.h>
using namespace std;
struct pos {
  int x, y, z;
};
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
queue<pos> Q;
char c[1002][1002];
bool visit[1002][1002];
inline bool ok(int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; }
int ans, f = 1e9;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'E') {
        pos lc;
        lc.x = i, lc.y = j, lc.z = 0;
        Q.push(lc);
      }
  while (!Q.empty()) {
    int i = Q.front().x;
    int j = Q.front().y;
    int k = Q.front().z;
    Q.pop();
    if (k > f) continue;
    if (c[i][j] == 'S') f = k;
    if (!visit[i][j] && c[i][j] != 'T' && ok(i, j)) {
      if (c[i][j] >= '0' && c[i][j] <= '9') ans += c[i][j] - '0';
      visit[i][j] = true;
      pos lc;
      for (int l = 0; l < 4; l++) {
        if (ok(dx[l] + i, dy[l] + j) && !visit[i + dx[l]][j + dy[l]]) {
          lc.x = dx[l] + i;
          lc.y = dy[l] + j;
          lc.z = k + 1;
          Q.push(lc);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
