#include <bits/stdc++.h>
using namespace std;
const int N = 1020;
int r, c, mark[N][N], tr[N][N], a[N][N], l, ans;
vector<pair<int, int> > v;
vector<pair<int, int> > p;
queue<pair<int, int> > q;
pair<int, int> s, e;
void bfs();
int main() {
  cin >> r >> c;
  for (int i = 0; i <= max(r, c); i++) {
    mark[i][0] = 1;
    mark[0][i] = 1;
  }
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) {
      char x;
      cin >> x;
      if (x == 'E') e = {i, j};
      if (x == 'S') s = {i, j};
      if (x == 'T') {
        tr[i][j] = 1;
        mark[i][j] = 1;
      } else if (x != '0' and x != 'S' and x != 'E') {
        a[i][j] = x - 48;
        v.push_back({i, j});
      }
    }
  mark[e.first][e.second] = 1;
  q.push(e);
  bfs();
  for (int i = 0; i < v.size(); i++) {
    if (mark[v[i].first][v[i].second] <= mark[s.first][s.second] and
        mark[v[i].first][v[i].second] > 0)
      ans += a[v[i].first][v[i].second];
  }
  cout << ans;
  return 0;
}
void bfs() {
  while (!(q.empty())) {
    int a = q.front().first, b = q.front().second;
    for (int i = -1; i <= 1; i++)
      for (int j = -1; j <= 1; j++)
        if ((a + i <= r and b + j <= c) and (a + i > 0 and b + j > 0))
          if ((i == 0 or j == 0) and mark[a + i][b + j] == 0 and
              !(i == 0 and j == 0)) {
            q.push({a + i, b + j});
            mark[a + i][b + j] = mark[a][b] + 1;
          }
    q.pop();
  }
}
