#include <bits/stdc++.h>
using namespace std;
string lab[1000];
int d[1000][1000];
int r, c;
queue<pair<int, int> > Q;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool good(int L, int x, int y) {
  if (x < 0 || y < 0 || x >= r || y >= c || lab[x][y] == 'T' || d[x][y] != -1)
    return 0;
  d[x][y] = L;
  return 1;
}
void BFS(int x, int y) {
  d[x][y] = 0;
  Q.push(make_pair(x, y));
  while (!Q.empty()) {
    x = (Q.front()).first;
    y = (Q.front()).second;
    Q.pop();
    for (int i = 0; i < 4; i++)
      if (good(d[x][y] + 1, x + dx[i], y + dy[i]))
        Q.push(make_pair(x + dx[i], y + dy[i]));
  }
}
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> lab[i];
  int x, y, sx, sy;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      d[i][j] = -1;
      if (lab[i][j] == 'E') {
        x = i;
        y = j;
      }
      if (lab[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  BFS(x, y);
  int ans = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (lab[i][j] >= '0' && lab[i][j] <= '9' && d[i][j] <= d[sx][sy] &&
          d[i][j] != -1)
        ans += lab[i][j] - '0';
  cout << ans;
}
