#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m, d[1011][1011];
char s[1011][1011];
inline bool inside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }
int main() {
  int sx, sy, ex, ey;
  scanf("%d %d", &n, &m);
  for (int i = 0, _n = (n); i < _n; i++) {
    scanf(" %s ", s[i]);
    for (int j = 0, _n = (m); j < _n; j++) {
      if (s[i][j] == 'E')
        ex = i, ey = j;
      else if (s[i][j] == 'S')
        sx = i, sy = j;
      d[i][j] = -1;
    }
  }
  queue<pair<int, int> > q;
  q.push(make_pair(ex, ey));
  d[ex][ey] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0, _n = (4); i < _n; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!inside(xx, yy)) continue;
      if (s[xx][yy] == 'T' || d[xx][yy] >= 0) continue;
      d[xx][yy] = d[x][y] + 1;
      q.push(make_pair(xx, yy));
    }
  }
  int res = 0;
  for (int i = 0, _n = (n); i < _n; i++)
    for (int j = 0, _n = (m); j < _n; j++)
      if ('0' <= s[i][j] && s[i][j] <= '9' && 0 <= d[i][j] &&
          d[i][j] <= d[sx][sy])
        res += s[i][j] - '0';
  printf("%d\n", res);
  return 0;
}
