#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int nax = 1000 + 10;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
char arr[nax][nax];
int dist[nax][nax];
int n, m;
int visited[nax][nax];
bool isvalid(int x, int y) {
  if (x <= 0 || y <= 0 || x > n || y > m) return false;
  if (arr[x][y] == 'T') return false;
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  int sx, sy, ex, ey;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S')
        sx = i, sy = j;
      else if (arr[i][j] == 'E')
        ex = i, ey = j;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) dist[i][j] = inf;
  }
  queue<int> q;
  q.push(ex);
  q.push(ey);
  dist[ex][ey] = 0;
  visited[ex][ey] = 1;
  while (!q.empty()) {
    int topx = q.front();
    q.pop();
    int topy = q.front();
    q.pop();
    for (int i = 0; i <= 3; i++) {
      for (int j = 0; j <= 3; j++) {
        int xx = topx + dx[i];
        int yy = topy + dy[i];
        if (isvalid(xx, yy) == false) continue;
        if (visited[xx][yy] == 0) {
          visited[xx][yy] = 1;
          dist[xx][yy] = 1 + dist[topx][topy];
          q.push(xx);
          q.push(yy);
        }
      }
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if ((i == sx && j == sy) || (i == ex && j == ey)) continue;
      if (dist[sx][sy] >= dist[i][j]) cnt += (int)(arr[i][j] - '0');
    }
  }
  cout << cnt;
  return 0;
}
