#include <bits/stdc++.h>
using namespace std;
struct point {
  int i, j, t;
} st, en;
struct pp {
  int t, v;
};
int n, m, tst, cnt = 0;
bool visited[1111][1111];
char a[1111][1111];
queue<point> q;
vector<pp> v;
bool check(int i, int j) {
  if (visited[i][j]) {
    return 0;
  }
  visited[i][j] = 1;
  return i && j && i <= n && j <= m && a[i][j] != 'T';
}
int main() {
  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] == 'E') {
        st = {i, j};
      }
      if (a[i][j] == 'S') {
        en = {i, j};
      }
    }
  }
  q.push({st.i, st.j, 0});
  while (!q.empty()) {
    int i = q.front().i, j = q.front().j, t = q.front().t;
    q.pop();
    if (i == en.i && j == en.j) {
      tst = t;
    } else {
      if (a[i][j] <= '9') {
        v.push_back({t, a[i][j] - '0'});
      }
    }
    if (check(i, j + 1)) {
      q.push({i, j + 1, t + 1});
    }
    if (check(i, j - 1)) {
      q.push({i, j - 1, t + 1});
    }
    if (check(i + 1, j)) {
      q.push({i + 1, j, t + 1});
    }
    if (check(i - 1, j)) {
      q.push({i - 1, j, t + 1});
    }
  }
  for (auto x : v) {
    if (x.t <= tst) {
      cnt += x.v;
    }
  }
  cout << cnt << '\n';
}
