#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const int N = 1050;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m;
char mx[N][N];
queue<pair<int, int> > Q;
int dist[N][N];
int main() {
  int e_x, e_y;
  int s_x, s_y;
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%c", &mx[i][j]);
      if (mx[i][j] == 'S') {
        s_x = i;
        s_y = j;
      }
      if (mx[i][j] == 'E') {
        e_x = i;
        e_y = j;
      }
    }
    scanf("\n");
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) dist[i][j] = INF;
  dist[e_x][e_y] = 0;
  Q.push(make_pair(e_x, e_y));
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    int x = p.first;
    int y = p.second;
    for (int i = 0; i < 4; i++) {
      int new_x = x + dx[i];
      int new_y = y + dy[i];
      if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < m &&
          mx[new_x][new_y] != 'T') {
        if (dist[new_x][new_y] > dist[x][y] + 1) {
          dist[new_x][new_y] = dist[x][y] + 1;
          Q.push(make_pair(new_x, new_y));
        }
      }
    }
  }
  int ans = 0;
  int max_dist = dist[s_x][s_y];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (mx[i][j] >= '0' && mx[i][j] <= '9' && dist[i][j] <= max_dist)
        ans += (int)(mx[i][j] - '0');
  printf("%d\n", ans);
  return 0;
}
