#include <bits/stdc++.h>
using namespace std;
const int dx[5] = {1, -1, 0, 0};
const int dy[5] = {0, 0, 1, -1};
int r, c, sx, sy, ex, ey, dis[1010][1010], vis[1010][1010];
char grid[1010][1010];
int ans = 0;
queue<pair<int, int> > q;
inline bool is_le(int x, int y) {
  if (vis[x][y] || x >= r || x < 0 || y >= c || y < 0 || grid[x][y] == 'T')
    return false;
  return true;
}
void bfs() {
  q.push(make_pair(ex, ey));
  vis[ex][ey] = 1;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int a = x + dx[i], b = y + dy[i];
      if (!is_le(a, b)) continue;
      dis[a][b] = dis[x][y] + 1;
      q.push(make_pair(a, b));
      vis[a][b] = 1;
    }
  }
  return;
}
int main() {
  scanf("%d%d", &r, &c);
  for (int i = 0; i < r; ++i) scanf("%s", grid[i]);
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (grid[i][j] == 'S') sx = i, sy = j;
      if (grid[i][j] == 'E') ex = i, ey = j;
    }
  }
  bfs();
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (isdigit(grid[i][j]) && dis[i][j] <= dis[sx][sy] && dis[i][j] != 0 &&
          grid[i][j] != '0')
        ans += (grid[i][j] - '0');
    }
  }
  printf("%d", ans);
  return 0;
}
