#include <bits/stdc++.h>
using namespace std;
string grid[1005];
int dist[1005][1005];
int dr[] = {0, 0, -1, 1};
int dc[] = {1, -1, 0, 0};
int main() {
  int n, m, x, y, ans = 0, r;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
    for (int j = 0; j < m; j++)
      if (grid[i][j] == 'E') x = i, y = j;
  }
  memset(dist, -1, sizeof dist);
  queue<pair<int, int>> q;
  dist[x][y] = 0;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    x = q.front().first, y = q.front().second;
    q.pop();
    if (grid[x][y] == 'S') r = dist[x][y];
    for (int i = 0; i < 4; i++) {
      int ii = x + dr[i];
      int jj = y + dc[i];
      if (ii < 0 || ii >= n || jj < 0 || jj >= m) continue;
      if (dist[ii][jj] != -1 || grid[ii][jj] == 'T') continue;
      dist[ii][jj] = dist[x][y] + 1;
      q.push(make_pair(ii, jj));
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (grid[i][j] >= '1' && grid[i][j] <= '9' && dist[i][j] <= r &&
          dist[i][j] != -1)
        ans += (grid[i][j] - '0');
  printf("%d\n", ans);
  return 0;
}
