#include <bits/stdc++.h>
using namespace std;
int n, m, sp[1010][1010];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char grid[1010][1010];
bool valid(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }
int bfs(int srcX, int srcY) {
  int dist, ans = 0;
  queue<pair<int, int>> q;
  memset(sp, -1, sizeof(sp));
  q.push({srcX, srcY});
  sp[srcX][srcY] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if (grid[x][y] == 'S') dist = sp[x][y];
    for (int i = 0; i < 4; ++i) {
      int toX = x + dx[i];
      int toY = y + dy[i];
      if (valid(toX, toY) && grid[toX][toY] != 'T' && sp[toX][toY] == -1) {
        q.push({toX, toY});
        sp[toX][toY] = sp[x][y] + 1;
      }
    }
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (grid[i][j] != 'T' && grid[i][j] != 'S' && grid[i][j] != 'E' &&
          sp[i][j] > -1 && sp[i][j] <= dist)
        ans += grid[i][j] - '0';
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%s", grid[i]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (grid[i][j] == 'E') return printf("%d\n", bfs(i, j)), 0;
  return 0;
}
