#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000")
#pragma warning(disable : 6031)
#pragma warning(disable : 4244)
#pragma warning(disable : 26451)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = int(1e9) + 7;
const int INF = 1e9;
const long long LINF = 1e18;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << setprecision(18) << fixed;
  int n, m;
  cin >> n >> m;
  vector<string> f(n);
  for (auto& x : f) cin >> x;
  int sx, sy, ex, ey;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (f[i][j] == 'S') {
        sx = i;
        sy = j;
      } else if (f[i][j] == 'E') {
        ex = i;
        ey = j;
      }
    }
  unordered_set<int> cur;
  auto to_int = [&](int x, int y) { return x * m + y; };
  auto to_pair = [&](int v) { return pair<int, int>{v / m, v % m}; };
  cur.insert(to_int(sx, sy));
  vector<vector<bool> > visited(n, vector<bool>(m, false));
  visited[sx][sy] = true;
  int min_dist = INT_MAX;
  for (int dist = 0;; dist++) {
    if (cur.count(to_int(ex, ey))) {
      min_dist = dist;
      break;
    }
    unordered_set<int> next;
    for (auto v : cur) {
      auto [x, y] = to_pair(v);
      vector<pair<int, int> > d = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
      for (auto [dx, dy] : d) {
        int cx = x + dx, cy = y + dy;
        if (cx < 0 || cy < 0 || cx >= n || cy >= m) continue;
        if (f[cx][cy] == 'T') continue;
        if (visited[cx][cy]) continue;
        visited[cx][cy] = true;
        next.insert(to_int(cx, cy));
      }
    }
    cur = next;
  }
  int res = 0;
  visited = vector<vector<bool> >(n, vector<bool>(m, false));
  visited[ex][ey] = true;
  cur.clear();
  cur.emplace(to_int(ex, ey));
  for (int dist = 0; dist < min_dist; dist++) {
    unordered_set<int> next;
    for (auto v : cur) {
      auto [x, y] = to_pair(v);
      vector<pair<int, int> > d = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
      for (auto [dx, dy] : d) {
        int cx = x + dx, cy = y + dy;
        if (cx < 0 || cy < 0 || cx >= n || cy >= m) continue;
        if (f[cx][cy] == 'T') continue;
        if (visited[cx][cy]) continue;
        if (isdigit(f[cx][cy])) res += f[cx][cy] - '0';
        visited[cx][cy] = true;
        next.insert(to_int(cx, cy));
      }
    }
    cur = next;
  }
  cout << res;
  return 0;
}
