#include <bits/stdc++.h>
using namespace std;
vector<vector<char>> grid;
vector<vector<int>> visited;
vector<vector<int>> distances;
int ex, ey;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m;
bool isValid(int x, int y) {
  if (x < 0 || y < 0 || x >= m || y >= n || visited[y][x] || grid[y][x] == 'T')
    return false;
  return true;
}
void bfs(int ex, int ey) {
  queue<pair<int, int>> q;
  q.push({ex, ey});
  distances[ey][ex] = 0;
  visited[ey][ex] = 1;
  while (!q.empty()) {
    pair<int, int> front = q.front();
    int x = front.first;
    int y = front.second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = dy[i] + y;
      int nx = dx[i] + x;
      if (isValid(nx, ny)) {
        visited[ny][nx] = 1;
        distances[ny][nx] = distances[y][x] + 1;
        q.push({nx, ny});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  int sx, sy;
  grid.resize(n, vector<char>(m));
  visited.resize(n, vector<int>(m, 0));
  distances.resize(n, vector<int>(m, INT_MAX));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') {
        sx = j;
        sy = i;
      }
      if (grid[i][j] == 'E') {
        ex = j;
        ey = i;
      }
    }
  bfs(ex, ey);
  int distance = distances[sy][sx];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int d = grid[i][j] - '0';
      if (d > 0 && d <= 9 && distances[i][j] <= distance) {
        ans += d;
      }
    }
  }
  cout << ans;
}
