#include <bits/stdc++.h>
using namespace std;
int r, c, s, e;
vector<int> adj[1000 * 1000 + 10];
int d[1000 * 1000 + 10];
char grid[1000][1000];
char access(int n) {
  int x = n % c;
  int y = n / c;
  return grid[y][x];
}
void bfs(int n) {
  queue<int> frontier;
  frontier.push(n);
  d[n] = 0;
  while (!frontier.empty()) {
    int u = frontier.front();
    frontier.pop();
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i];
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        frontier.push(v);
      }
    }
  }
}
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> grid[i][j];
    }
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      int cell = i * c + j;
      if (grid[i][j] == 'T') continue;
      if (grid[i][j] == 'S')
        s = cell;
      else if (grid[i][j] == 'E')
        e = cell;
      if (grid[i - 1][j] != 'T' && i > 0) {
        adj[cell].push_back(cell - c);
        adj[cell - c].push_back(cell);
      }
      if (grid[i + 1][j] != 'T' && i < r - 1) {
        adj[cell].push_back(cell + c);
        adj[cell + c].push_back(cell);
      }
      if (grid[i][j - 1] != 'T' && j > 0) {
        adj[cell].push_back(cell - 1);
        adj[cell - 1].push_back(cell);
      }
      if (grid[i][j + 1] != 'T' && j < c - 1) {
        adj[cell].push_back(cell + 1);
        adj[cell + 1].push_back(cell);
      }
    }
  }
  memset(d, -1, sizeof(d));
  bfs(e);
  int shortest_path = d[s];
  int ans = 0;
  for (int i = 0; i < r * c; i++) {
    char in = access(i);
    if (in == 'T' || in == 'E' || in == 'S') continue;
    if (d[i] <= shortest_path && d[i] != -1) {
      ans += in - '0';
    }
  }
  cout << ans << endl;
  return 0;
}
