#include <bits/stdc++.h>
using namespace std;
char c[1005][1005];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, d[1005][1005],
    vis[1005][1005];
int main() {
  int n, m, x, y, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'E') x = i, y = j;
      if (c[i][j] == 'S') a = i, b = j;
    }
  }
  queue<pair<int, int> > q;
  memset(d, 0x3f, sizeof(d));
  q.push({x, y});
  d[x][y] = 0;
  vis[x][y] = 0;
  while (!q.empty()) {
    int sx = q.front().first, sy = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + sx, ny = dy[i] + sy;
      if (nx > n || nx < 1 || ny > m || ny < 1 || c[nx][ny] == 'T' ||
          vis[nx][ny])
        continue;
      d[nx][ny] = min(d[nx][ny], d[sx][sy] + 1);
      vis[nx][ny] = 1;
      q.push({nx, ny});
    }
  }
  int num = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (c[i][j] >= '1' && c[i][j] <= '9') {
        if (d[i][j] <= d[a][b]) num += (c[i][j] - '0');
      }
    }
  }
  cout << num << endl;
}
