#include <bits/stdc++.h>
using namespace std;
int n, m;
string S[1005];
int dist[1005][1005];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  ios_base::sync_with_stdio(false);
  while (cin >> n >> m) {
    for (int i = 0; i < (n); i++) cin >> S[i];
    for (int i = 0; i < (n); i++)
      for (int j = 0; j < (m); j++) dist[i][j] = (1 << 30);
    pair<int, int> s, e;
    int ans = 0;
    queue<pair<int, int> > Q;
    for (int i = 0; i < (n); i++)
      for (int j = 0; j < (m); j++)
        if (S[i][j] == 'E') {
          Q.push(make_pair(i, j));
          dist[i][j] = 0;
          e = make_pair(i, j);
        } else if (S[i][j] == 'S')
          s = make_pair(i, j);
    while (!Q.empty()) {
      pair<int, int> node = Q.front();
      Q.pop();
      int x = node.first, y = node.second;
      for (int k = 0; k < (4); k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m &&
            dist[nx][ny] == (1 << 30) && S[nx][ny] != 'T') {
          Q.push(make_pair(nx, ny));
          dist[nx][ny] = dist[x][y] + 1;
        }
      }
    }
    int t = dist[s.first][s.second];
    for (int i = 0; i < (n); i++)
      for (int j = 0; j < (m); j++)
        if (S[i][j] >= '1' && S[i][j] <= '9' && dist[i][j] <= t)
          ans += S[i][j] - '0';
    printf("%d\n", ans);
  }
}
