#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
char arr[N][N];
int level[N][N];
bool visited[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  level[x][y] = 0;
  visited[x][y] = 1;
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop();
    for (int a = 0; a < 4; a++) {
      int newx = i + dx[a];
      int newy = j + dy[a];
      if (newx < 0 || newx > n - 1 || newy < 0 || newy > m - 1) continue;
      if (visited[newx][newy]) continue;
      if (arr[newx][newy] == 'T') continue;
      visited[newx][newy] = 1;
      q.push({newx, newy});
      level[newx][newy] = level[i][j] + 1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  pair<int, int> start;
  pair<int, int> exit;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        start.first = i;
        start.second = j;
      } else if (arr[i][j] == 'E') {
        exit.first = i;
        exit.second = j;
      }
    }
  }
  bfs(exit.first, exit.second);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] != '0' && arr[i][j] != 'S' && arr[i][j] != 'E' &&
          arr[i][j] != 'T') {
        if (visited[i][j] && level[i][j] <= level[start.first][start.second])
          ans += (arr[i][j] - '0');
      }
    }
  }
  cout << ans << "\n";
}
