#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
char a[N][N];
int n, m, ans, d[N][N];
int way[2][4] = {{1, -1, 0, 0}, {0, 0, -1, 1}};
queue<pair<int, int> > q;
pair<int, int> start, finish;
vector<pair<int, int> > b;
bool valid(int x, int y) {
  if (x < 1 || x > n) return 0;
  if (y < 1 || y > m) return 0;
  if (a[x][y] == 'T') return 0;
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') start = make_pair(i, j);
      if (a[i][j] == 'E') finish = make_pair(i, j);
      if (a[i][j] >= '0' && a[i][j] <= '9') b.push_back(make_pair(i, j));
    }
    fill(d[i], d[i] + N, N * N);
  }
  q.push(finish);
  d[finish.first][finish.second] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = x + way[0][i], ty = y + way[1][i];
      if (valid(tx, ty) && d[x][y] + 1 < d[tx][ty]) {
        d[tx][ty] = d[x][y] + 1;
        q.push(make_pair(tx, ty));
      }
    }
  }
  int dis = d[start.first][start.second];
  for (int i = 0; i < b.size(); i++) {
    int x = b[i].first, y = b[i].second, k = a[x][y] - '0';
    if (d[x][y] > dis) continue;
    ans += k;
  }
  cout << ans << endl;
  return 0;
}
