#include <bits/stdc++.h>
using namespace std;
int n, m, ans, check;
int a[5555][5555];
char c[5555][5555];
bool used[5555][5555];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
queue<pair<int, int> > q;
pair<int, int> solve(int x, int y, int k) {
  int tx = x + dx[k], ty = y + dy[k];
  if (tx == 0 or tx == n + 1 or ty == 0 or ty == m + 1 or c[tx][ty] == 'T') {
    return pair<int, int>(x, y);
  }
  return pair<int, int>(tx, ty);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'E') {
        q.push(pair<int, int>(i, j));
        a[i][j] = 0;
        used[i][j] = true;
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int x = tmp.first, y = tmp.second, step = a[tmp.first][tmp.second];
    for (int i = 0; i < 4; i++) {
      pair<int, int> now = solve(x, y, i);
      if (!used[now.first][now.second]) {
        used[now.first][now.second] = true;
        q.push(pair<int, int>(now.first, now.second));
        a[now.first][now.second] = step + 1;
        if (c[now.first][now.second] == 'S') {
          check = a[now.first][now.second];
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] <= check and c[i][j] > '0' and c[i][j] <= '9' and
          a[i][j] != 0) {
        ans += (int)(c[i][j] - '0');
      }
    }
  }
  cout << ans;
  return 0;
}
