#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
char s[N][N];
int n, m, dist[N][N];
int main() {
  memset(dist, 0x3f, sizeof dist);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  pair<int, int> st, ed;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S')
        ed = {i, j};
      else if (s[i][j] == 'E')
        st = {i, j};
  queue<pair<int, int> > q;
  dist[st.first][st.second] = 0;
  q.push(st);
  while (q.size()) {
    auto it = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int first = it.first + dx[i], second = it.second + dy[i];
      if (first < 1 || first > n || second < 1 || second > m) continue;
      if (dist[first][second] != 0x3f3f3f3f || s[first][second] == 'T')
        continue;
      dist[first][second] = dist[it.first][it.second] + 1;
      q.push({first, second});
    }
  }
  int temp = dist[ed.first][ed.second];
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] <= '9' && s[i][j] >= '0') {
        if (dist[i][j] <= temp && dist[i][j] != 0x3f3f3f3f)
          ans += s[i][j] - '0';
      }
    }
  }
  cout << ans << endl;
  return 0;
}
