#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
int R, C, frow, fcol, row, col, srow, scol, nxcol, nxrow;
int ans, intest[1005][1005], goal;
char grid[1005][1005];
queue<pair<int, int> > q;
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, -1, 0, 1};
bool inside(int r, int c) { return r > 0 && c > 0 && r <= R && c <= C; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> R >> C;
  for (int r = 1; r <= R; r++) {
    for (int c = 1; c <= C; c++) {
      cin >> grid[r][c];
      intest[r][c] = INF;
      if (grid[r][c] == 'S')
        srow = r, scol = c;
      else if (grid[r][c] == 'E')
        frow = r, fcol = c;
    }
  }
  intest[frow][fcol] = 0;
  q.push(make_pair(frow, fcol));
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    row = cur.first;
    col = cur.second;
    for (int i = 0; i <= 3; i++) {
      nxrow = row + dr[i];
      nxcol = col + dc[i];
      if (!inside(nxrow, nxcol)) continue;
      if (grid[nxrow][nxcol] == 'T') continue;
      if (intest[nxrow][nxcol] != INF) continue;
      intest[nxrow][nxcol] = intest[row][col] + 1;
      q.push(make_pair(nxrow, nxcol));
    }
  }
  goal = intest[srow][scol];
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      if (grid[i][j] - '0' > 0 && grid[i][j] < 58) {
        srow = i, scol = j;
        if (intest[srow][scol] <= goal) ans += (grid[i][j] - '0');
      }
    }
  }
  cout << ans << endl;
  return 0;
}
