#include <bits/stdc++.h>
using namespace std;
long long n, m, stx, sty, enx, eny;
char arr[1005][1005];
queue<pair<long long, long long> > q;
long long vis[1005][1005];
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    for (long long j = (long long)(1); j <= (long long)(m); j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'E') {
        enx = i;
        eny = j;
      }
      if (arr[i][j] == 'S') {
        stx = i;
        sty = j;
      }
    }
  }
  memset(vis, -1, sizeof(vis));
  vis[enx][eny] = 1;
  q.push(make_pair(enx, eny));
  while (!q.empty()) {
    long long x = q.front().first;
    long long y = q.front().second;
    q.pop();
    if (x - 1 >= 1 && vis[x - 1][y] == -1 && arr[x - 1][y] != 'T') {
      q.push(make_pair(x - 1, y));
      vis[x - 1][y] = vis[x][y] + 1;
    }
    if (y - 1 >= 1 && vis[x][y - 1] == -1 && arr[x][y - 1] != 'T') {
      q.push(make_pair(x, y - 1));
      vis[x][y - 1] = vis[x][y] + 1;
    }
    if (x + 1 <= n && vis[x + 1][y] == -1 && arr[x + 1][y] != 'T') {
      q.push(make_pair(x + 1, y));
      vis[x + 1][y] = vis[x][y] + 1;
    }
    if (y + 1 <= m && vis[x][y + 1] == -1 && arr[x][y + 1] != 'T') {
      q.push(make_pair(x, y + 1));
      vis[x][y + 1] = vis[x][y] + 1;
    }
  }
  long long ans = 0;
  long long mintime = vis[stx][sty];
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    for (long long j = (long long)(1); j <= (long long)(m); j++) {
      if (vis[i][j] <= mintime && arr[i][j] >= '0' && arr[i][j] <= '9' &&
          vis[i][j] != -1)
        ans += (arr[i][j] - '0');
    }
  }
  cout << ans << endl;
  return 0;
}
