#include <bits/stdc++.h>
using namespace std;
char grid[1010][1010];
long long dist[1010][1010], r, c;
void bfs(int sx, int sy) {
  queue<pair<int, int> > q;
  q.push({sx, sy});
  dist[sx][sy] = 1;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x + 1 <= r and !dist[x + 1][y] and grid[x + 1][y] != 'T') {
      dist[x + 1][y] = dist[x][y] + 1;
      q.push({x + 1, y});
    }
    if (x - 1 >= 1 and !dist[x - 1][y] and grid[x - 1][y] != 'T') {
      dist[x - 1][y] = dist[x][y] + 1;
      q.push({x - 1, y});
    }
    if (y + 1 <= c and !dist[x][y + 1] and grid[x][y + 1] != 'T') {
      dist[x][y + 1] = dist[x][y] + 1;
      q.push({x, y + 1});
    }
    if (y - 1 >= 1 and !dist[x][y - 1] and grid[x][y - 1] != 'T') {
      dist[x][y - 1] = dist[x][y] + 1;
      q.push({x, y - 1});
    }
  }
}
int main() {
  cin >> r >> c;
  for (int i = 1; i <= r; ++i) {
    for (int j = 1; j <= c; ++j) cin >> grid[i][j];
  }
  int sx, sy;
  for (int i = 1; i <= r; ++i) {
    for (int j = 1; j <= c; ++j) {
      if (grid[i][j] == 'E') {
        bfs(i, j);
      }
      if (grid[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  long long sum = 0;
  for (int i = 1; i <= r; ++i) {
    for (int j = 1; j <= c; ++j) {
      if (dist[i][j] and grid[i][j] != 'T' and grid[i][j] != 'E' and
          grid[i][j] != 'S' and grid[i][j] != '0') {
        if (max(dist[sx][sy] - 1, 0LL) >= max(dist[i][j] - 1, 0LL))
          sum += grid[i][j] - '0';
      }
    }
  }
  cout << sum;
  return 0;
}
