#include <bits/stdc++.h>
using namespace std;
char grid[1005][1005];
int dist[1005][1005];
bool visit[1005][1005];
int tx[4] = {0, 1, 0, -1};
int ty[4] = {1, 0, -1, 0};
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  pair<int, int> start, exit;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == 'S') {
        start = make_pair(i, j);
      } else if (grid[i][j] == 'E') {
        exit = make_pair(i, j);
      }
    }
  }
  queue<pair<int, int> > q;
  q.push(exit);
  dist[start.first][start.second] = 0;
  while (!q.empty()) {
    pair<int, int> temp = q.front();
    q.pop();
    int x = temp.first;
    int y = temp.second;
    if (visit[x][y]) {
      continue;
    }
    visit[x][y] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = x + tx[i];
      int ny = y + ty[i];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] != 'T') {
        if (visit[nx][ny] != 1) {
          dist[nx][ny] = dist[x][y] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  int ourTime = dist[start.first][start.second];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] <= ourTime && grid[i][j] != 'S' && grid[i][j] != 'E' &&
          grid[i][j] != 'T' && visit[i][j] == 1) {
        ans += grid[i][j] - '0';
      }
    }
  }
  cout << ans;
  return 0;
}
