#include <bits/stdc++.h>
using namespace std;
char mat[1001][1001];
int dis[1001][1001];
bool vis[1001][1001];
int n, m;
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i <= (n - 1); i++) scanf(" %s", mat[i]);
  memset(dis, -1, sizeof(dis));
  int sti, stj, pli, plj;
  for (int i = 0; i <= (n - 1); i++)
    for (int j = 0; j <= (m - 1); j++)
      if (mat[i][j] == 'E') {
        sti = i;
        stj = j;
      } else if (mat[i][j] == 'S') {
        pli = i;
        plj = j;
      }
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair(make_pair(sti, stj), 0));
  dis[sti][stj] = 0;
  while (!q.empty()) {
    pair<pair<int, int>, int> temp = q.front();
    q.pop();
    int x = temp.first.first, y = temp.first.second, d = temp.second;
    if (x < 0 || x >= n || y < 0 || y >= m || mat[x][y] == 'T' || vis[x][y])
      continue;
    dis[x][y] = d;
    vis[x][y] = true;
    q.push(make_pair(make_pair(x + 1, y), d + 1));
    q.push(make_pair(make_pair(x - 1, y), d + 1));
    q.push(make_pair(make_pair(x, y + 1), d + 1));
    q.push(make_pair(make_pair(x, y - 1), d + 1));
  }
  long long ans = 0;
  int ref = dis[pli][plj];
  for (int i = 0; i <= (n - 1); i++) {
    for (int j = 0; j <= (m - 1); j++) {
      if (dis[i][j] != -1 && mat[i][j] != 'S' && mat[i][j] != 'E' &&
          dis[i][j] <= ref)
        ans = ans + (mat[i][j] - '0');
    }
  }
  cout << ans << endl;
  return 0;
}
