#include <bits/stdc++.h>
using namespace std;
int n, m, cnt;
vector<pair<int, int> > V;
char grid[1001][1001];
int dis[1001][1001];
queue<pair<int, int> > q;
bool vis[1001][1001];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool ok(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m) return false;
  return grid[x][y] != 'T';
}
void bfs(int x, int y) {
  q.push(make_pair(x, y));
  vis[x][y] = 1;
  dis[x][y] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      pair<int, int> xi;
      xi.first = p.first + dx[i];
      xi.second = p.second + dy[i];
      if (ok(xi.first, xi.second) &&
          dis[xi.first][xi.second] > dis[p.first][p.second] + 1) {
        q.push(xi);
        dis[xi.first][xi.second] = dis[p.first][p.second] + 1;
      }
    }
  }
}
int main() {
  int xs, xe, ys, ye;
  vector<pair<int, int> > P;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> grid[i][j];
      dis[i][j] = 100000000;
      if (grid[i][j] == 'S') {
        xs = i;
        ys = j;
      }
      if (grid[i][j] == 'E') {
        xe = i;
        ye = j;
      }
      if (isdigit(grid[i][j])) P.push_back(make_pair(i, j));
    }
  }
  bfs(xe, ye);
  for (int i = 0; i < P.size(); i++) {
    if (dis[P[i].first][P[i].second] <= dis[xs][ys])
      cnt += grid[P[i].first][P[i].second] - '0';
  }
  cout << cnt;
}
