#include <bits/stdc++.h>
using namespace std;
int visited[1010][1010], dist[1010][1010];
int main() {
  int ei, ej, si, sj, st, n, m, i, j, g, h;
  cin >> n >> m;
  char c[n + 1][m + 1];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'E') {
        ei = i;
        ej = j;
      }
      if (c[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  queue<pair<int, int> > q;
  q.push(make_pair(ei, ej));
  dist[ei][ej] = 0;
  visited[ei][ej] = 1;
  while (!q.empty()) {
    g = (q.front()).first;
    h = (q.front()).second;
    q.pop();
    if (((g + 1) >= 0) && ((h) >= 0) && ((g + 1) <= (n - 1)) &&
        ((h) <= m - 1)) {
      if (!visited[g + 1][h] && (c[g + 1][h] != 'T')) {
        q.push(make_pair(g + 1, h));
        visited[g + 1][h] = 1;
        dist[g + 1][h] = dist[g][h] + 1;
      }
    }
    if (((g - 1) >= 0) && ((h) >= 0) && ((g - 1) <= (n - 1)) &&
        ((h) <= m - 1)) {
      if (!visited[g - 1][h] && (c[g - 1][h] != 'T')) {
        q.push(make_pair(g - 1, h));
        visited[g - 1][h] = 1;
        dist[g - 1][h] = dist[g][h] + 1;
      }
    }
    if (((g) >= 0) && ((h + 1) >= 0) && ((g) <= (n - 1)) &&
        ((h + 1) <= m - 1)) {
      if (!visited[g][h + 1] && (c[g][h + 1] != 'T')) {
        q.push(make_pair(g, h + 1));
        visited[g][h + 1] = 1;
        dist[g][h + 1] = dist[g][h] + 1;
      }
    }
    if (((g) >= 0) && ((h - 1) >= 0) && ((g) <= (n - 1)) &&
        ((h - 1) <= m - 1)) {
      if (!visited[g][h - 1] && (c[g][h - 1] != 'T')) {
        q.push(make_pair(g, h - 1));
        visited[g][h - 1] = 1;
        dist[g][h - 1] = dist[g][h] + 1;
      }
    }
  }
  st = dist[si][sj];
  int ans = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if ((dist[i][j] <= st) && (c[i][j] != 'T') && (c[i][j] != 'S') &&
          (c[i][j] != 'E') && visited[i][j]) {
        ans += c[i][j] - '0';
      }
    }
  }
  cout << ans;
}
