#include <bits/stdc++.h>
using namespace std;
int r, c, sx, sy, ex, ey, dist[1005][1005], dx[] = {-1, 0, 1, 0},
                                            dy[] = {0, -1, 0, 1};
bool vis[1005][1005];
char mp[1005][1005];
int main() {
  cin >> r >> c;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') sx = i, sy = j;
      if (mp[i][j] == 'E') ex = i, ey = j;
    }
  memset(dist, -1, sizeof(dist));
  memset(vis, 0, sizeof(vis));
  dist[ex][ey] = 0;
  vis[ex][ey] = 1;
  queue<pair<int, int> > q;
  q.push(make_pair(ex, ey));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = p.first + dx[i], yy = p.second + dy[i];
      if (xx < 1 || xx > r || yy < 1 || yy > c || vis[xx][yy] ||
          mp[xx][yy] == 'T')
        continue;
      vis[xx][yy] = 1;
      dist[xx][yy] = dist[p.first][p.second] + 1;
      q.push(make_pair(xx, yy));
    }
  }
  int ans = 0;
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++)
      if (isdigit(mp[i][j]) && mp[i][j] - '0' > 0 &&
          dist[i][j] <= dist[sx][sy] && dist[i][j] != -1)
        ans += mp[i][j] - '0';
  cout << ans << endl;
  return 0;
}
