#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, m;
int adj[N][N], d[N][N];
bool check(int a, int b) {
  if (a >= 0 && a < n && b >= 0 && b < m && adj[a][b] != -1) return 1;
  return 0;
}
void bfs(int x, int y) {
  d[x][y] = 0;
  queue<pair<int, int> > q;
  q.push({x, y});
  while (!q.empty()) {
    int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ii = dir[i][0] + p.first;
      int jj = dir[i][1] + p.second;
      if (check(ii, jj) && d[ii][jj] == -1) {
        q.push({ii, jj});
        d[ii][jj] = d[p.first][p.second] + 1;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) d[i][j] = -1;
  pair<int, int> e, s;
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == 'E') {
        e.first = i;
        e.second = j;
        adj[i][j] = 0;
      } else if (c == 'S') {
        s.first = i;
        s.second = j;
        adj[i][j] = 0;
      } else if (c == 'T')
        adj[i][j] = -1;
      else
        adj[i][j] = (c - '0');
    }
  }
  bfs(e.first, e.second);
  int len = d[s.first][s.second];
  long long int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (d[i][j] != -1 && d[i][j] <= len) ans += adj[i][j];
  cout << ans << endl;
}
