#include <bits/stdc++.h>
using namespace std;
char mp[1005][1005];
bool vis[1005][1005];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r, c, sx, sy, ex, ey, shortest, ans = 0;
void bfs1() {
  queue<tuple<int, int, int>> q;
  q.push(make_tuple(ex, ey, 0));
  while (!q.empty()) {
    int curx = get<0>(q.front());
    int cury = get<1>(q.front());
    int dis = get<2>(q.front());
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int nx = curx + dx[k], ny = cury + dy[k];
      if (nx > 0 && nx <= r && ny > 0 && ny <= c && !vis[nx][ny] &&
          mp[nx][ny] != 'T') {
        if (mp[nx][ny] == 'S') {
          shortest = dis + 1;
        }
        vis[nx][ny] = 1;
        q.push(make_tuple(nx, ny, dis + 1));
      }
    }
  }
}
void bfs2() {
  queue<tuple<int, int, int>> q;
  q.push(make_tuple(ex, ey, 0));
  while (!q.empty()) {
    int curx = get<0>(q.front());
    int cury = get<1>(q.front());
    int dis = get<2>(q.front());
    q.pop();
    if (mp[curx][cury] - '0' <= 9) {
      ans += (mp[curx][cury] - '0');
    }
    for (int k = 0; k < 4; ++k) {
      int nx = curx + dx[k], ny = cury + dy[k];
      if (nx > 0 && nx <= r && ny > 0 && ny <= c && !vis[nx][ny] &&
          mp[nx][ny] != 'T' && dis < shortest) {
        vis[nx][ny] = 1;
        q.push(make_tuple(nx, ny, dis + 1));
      }
    }
  }
}
int main() {
  cin >> r >> c;
  for (int i = 1; i <= r; ++i) {
    for (int j = 1; j <= c; ++j) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') {
        sx = i;
        sy = j;
      } else if (mp[i][j] == 'E') {
        ex = i;
        ey = j;
      }
    }
  }
  bfs1();
  memset(vis, 0, sizeof(vis));
  bfs2();
  cout << ans << "\n";
}
