#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = INT_MAX;
const long long LINF = LLONG_MAX;
const int N = 1e3 + 20;
string a[N];
int n, m, g[N][N], d[N][N], ans;
pair<int, int> root, t;
bool visited[N][N];
queue<pair<int, int> > q;
void bfs(pair<int, int> src) {
  d[src.first][src.second] = 0;
  q.push(src);
  while (q.size()) {
    pair<int, int> v = q.front();
    vector<pair<int, int> > h = {{v.first + 1, v.second},
                                 {v.first - 1, v.second},
                                 {v.first, v.second + 1},
                                 {v.first, v.second - 1}};
    for (auto u : h)
      if (!visited[u.first][u.second]) {
        visited[u.first][u.second] = 1;
        d[u.first][u.second] = d[v.first][v.second] + 1;
        q.push(u);
      }
    q.pop();
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) g[i][j] = -1, d[i][j] = INF;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'E') {
        g[i + 1][j + 1] = -3;
        root = {i + 1, j + 1};
      } else if (a[i][j] == 'S') {
        g[i + 1][j + 1] = -2;
        t = {i + 1, j + 1};
      } else if (a[i][j] == 'T')
        g[i + 1][j + 1] = -1;
      else
        g[i + 1][j + 1] = (a[i][j] - '0');
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      if (g[i][j] == -1) visited[i][j] = 1;
  bfs(root);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (g[i][j] > 0 && d[i][j] <= d[t.first][t.second]) ans += g[i][j];
  cout << ans << endl;
}
