#include <bits/stdc++.h>
using namespace std;
int v[1005][1005];
char a[1005][1005];
int n, m;
int bfs(int ans, int i, int j, bool flag) {
  queue<pair<int, int> > q;
  long long int total = 0;
  pair<int, int> x;
  q.push(pair<int, int>(i, j));
  v[i][j] = 1;
  while (q.size() > 0) {
    x = q.front();
    if (flag) {
      if (a[x.first][x.second] == 'E') return v[x.first][x.second];
    } else {
      if (v[x.first][x.second] > ans) break;
      if (a[x.first][x.second] > '0' && a[x.first][x.second] <= '9')
        total += a[x.first][x.second] - '0';
    }
    q.pop();
    for (short p = -1; p <= 1; p += 2) {
      if (x.first + p >= n || x.first + p < 0) continue;
      if (a[x.first + p][x.second] == 'T' || v[x.first + p][x.second]) continue;
      v[x.first + p][x.second] = 1 + v[x.first][x.second];
      q.push(pair<int, int>(x.first + p, x.second));
    }
    for (short p = -1; p <= 1; p += 2) {
      if (x.second + p >= m || x.second + p < 0) continue;
      if (a[x.first][x.second + p] == 'T' || v[x.first][p + x.second]) continue;
      v[x.first][x.second + p] = 1 + v[x.first][x.second];
      q.push(pair<int, int>(x.first, p + x.second));
    }
  }
  return flag ? ans : total;
}
int main() {
  int ans;
  cin >> n >> m;
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'S') {
        ans = bfs(0, i, j, 1);
        break;
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) v[i][j] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'E') {
        cout << bfs(ans, i, j, 0);
        break;
      }
  return 0;
}
