#include <bits/stdc++.h>
using namespace std;
long long ans, n, m, ya3[1005][1005], cost[1005][1005], sti, stj, eni, enj,
    x[4] = {0, 0, 1, -1}, y[4] = {1, -1, 0, 0}, tim;
bool vis[1005][1005];
char a[1005][1005];
vector<pair<int, int> > v;
bool isValid(int i, int j) {
  if (i < 1 || i > n || j < 1 || j > m || a[i][j] == 'T' || vis[i][j]) return 0;
  return 1;
}
void bfs() {
  queue<pair<int, int> > q;
  q.push({sti, stj});
  vis[sti][stj] = 1;
  cost[sti][stj] = 0;
  while (q.size()) {
    int i = q.front().first, j = q.front().second;
    q.pop();
    for (int k = 0; k < 4; k++) {
      int ii = i + x[k], jj = j + y[k];
      if (isValid(ii, jj)) {
        vis[ii][jj] = 1;
        cost[ii][jj] = cost[i][j] + 1;
        q.push({ii, jj});
      }
    }
  }
}
void bfs2() {
  memset(cost, 0, sizeof(cost));
  memset(vis, 0, sizeof(vis));
  queue<pair<int, int> > q;
  q.push({eni, enj});
  while (q.size()) {
    int i = q.front().first, j = q.front().second;
    if (cost[i][j] <= tim) ans += ya3[i][j];
    q.pop();
    for (int k = 0; k < 4; k++) {
      int ii = i + x[k], jj = j + y[k];
      if (isValid(ii, jj)) {
        vis[ii][jj] = 1;
        cost[ii][jj] = cost[i][j] + 1;
        q.push({ii, jj});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sti = i, stj = j;
      } else if (a[i][j] == 'E') {
        eni = i, enj = j;
      } else if (a[i][j] >= '0' && a[i][j] <= '9') {
        ya3[i][j] = a[i][j] - '0';
      }
    }
  }
  bfs();
  tim = cost[eni][enj];
  bfs2();
  cout << ans << "\n";
  return 0;
}
