#include <bits/stdc++.h>
using namespace std;
const int MAX = 1010;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char arr[MAX][MAX];
int vis[MAX][MAX];
struct P {
  int x, y;
} p_s, p_e;
int m, n;
int BFS(P p1) {
  queue<P> q;
  q.push(p1);
  vis[p1.x][p1.y] = 0;
  while (!q.empty()) {
    P p2 = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      P p3;
      p3.x = p2.x + dx[i];
      p3.y = p2.y + dy[i];
      if (0 <= p3.x && p3.x < m && p3.y >= 0 && p3.y < n &&
          vis[p3.x][p3.y] == -1 && arr[p3.x][p3.y] != 'T') {
        vis[p3.x][p3.y] = vis[p2.x][p2.y] + 1;
        q.push(p3);
      }
    }
  }
  return vis[p_s.x][p_s.y];
}
int main() {
  scanf("%d%d", &m, &n);
  int cnt = 0;
  memset(vis, -1, sizeof(vis));
  for (int i = 0; i < m; i++) scanf("%s", &arr[i]);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (arr[i][j] == 'E') {
        p_e.x = i;
        p_e.y = j;
      }
      if (arr[i][j] == 'S') {
        p_s.x = i;
        p_s.y = j;
      }
    }
  }
  int ans = 0;
  int res = BFS(p_e);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (arr[i][j] >= '1' && arr[i][j] <= '9' && vis[i][j] != -1) {
        if (vis[i][j] <= res) ans += arr[i][j] - '0';
      }
    }
  }
  cout << ans;
  return 0;
}
