#include <bits/stdc++.h>
using namespace std;
struct poi {
  int x, y;
};
int n, m, ans = 0;
int stx, sty, edx, edy;
char map2[1010][1010];
int dis[1010][1010];
bool visit[1010][1010];
int main() {
  queue<poi> que;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> map2[i][j];
      visit[i][j] = 1, dis[i][j] = 0;
      if (map2[i][j] != 'T') visit[i][j] = 0;
      if (map2[i][j] == 'S') stx = i, sty = j;
      if (map2[i][j] == 'E') edx = i, edy = j;
    }
  }
  dis[edx][edy] = 0;
  visit[edx][edy] = 1;
  poi temp;
  temp.x = edx, temp.y = edy;
  que.push(temp);
  while (!que.empty()) {
    poi u = que.front();
    que.pop();
    int x, y;
    x = u.x, y = u.y;
    if (x + 1 <= n && visit[x + 1][y] == 0) {
      temp.x = x + 1, temp.y = y;
      dis[x + 1][y] = dis[x][y] + 1;
      visit[x + 1][y] = 1;
      que.push(temp);
    }
    if (x - 1 >= 1 && visit[x - 1][y] == 0) {
      temp.x = x - 1, temp.y = y;
      dis[x - 1][y] = dis[x][y] + 1;
      visit[x - 1][y] = 1;
      que.push(temp);
    }
    if (y + 1 <= m && visit[x][y + 1] == 0) {
      temp.x = x, temp.y = y + 1;
      dis[x][y + 1] = dis[x][y] + 1;
      visit[x][y + 1] = 1;
      que.push(temp);
    }
    if (y - 1 >= 1 && visit[x][y - 1] == 0) {
      temp.x = x, temp.y = y - 1;
      dis[x][y - 1] = dis[x][y] + 1;
      visit[x][y - 1] = 1;
      que.push(temp);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (map2[i][j] >= '0' && map2[i][j] <= '9' &&
          dis[i][j] <= dis[stx][sty] && dis[i][j] != 0)
        ans += (int(map2[i][j]) - '0');
    }
  }
  cout << ans;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
