#include <bits/stdc++.h>
using namespace std;
const int N = 1012, INF = 1e9;
int n, m, ex, ey, sx, sy;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int dis[N][N];
string s[N];
void bfs(int X, int Y) {
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) dis[i][j] = INF;
  q.push({X, Y});
  dis[X][Y] = 0;
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
      if (s[nx][ny] == 'T') continue;
      if (dis[nx][ny] > dis[x][y] + 1) {
        dis[nx][ny] = dis[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'E') ex = i, ey = j;
      if (s[i][j] == 'S') sx = i, sy = j;
    }
  }
  bfs(ex, ey);
  int val = dis[sx][sy], ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dis[i][j] <= val && (s[i][j] >= '0' && s[i][j] <= '9')) {
        ans += s[i][j] - '0';
      }
    }
  }
  cout << ans;
  return 0;
}
