#include <bits/stdc++.h>
using namespace std;
struct node {
  int r, c, p;
  node() {}
  node(int _r, int _c, int _p) { r = _r, c = _c, p = _p; }
};
const int MAXN = 1005;
char g[MAXN][MAXN];
int dist[MAXN][MAXN];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int to_int(const char& c) { return c - '0'; }
bool is_digit(const char& c) {
  if ('1' <= c && c <= '9') return true;
  return false;
}
void bfs(const int& r, const int& c, node s) {
  queue<node> q;
  q.push(s);
  memset(dist, -1, sizeof dist);
  dist[s.r][s.c] = 0;
  int xx, yy;
  while (!q.empty()) {
    s = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      xx = dx[i] + s.r;
      yy = dy[i] + s.c;
      if (xx < 0 || xx >= r || yy < 0 || yy >= c || g[xx][yy] == 'T' ||
          dist[xx][yy] != -1)
        continue;
      dist[xx][yy] = dist[s.r][s.c] + 1;
      q.emplace(xx, yy, s.p);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int r, c, best, ans;
  vector<node> b;
  node s, f;
  while (cin >> r >> c) {
    for (int i = 0; i < r; ++i) {
      for (int j = 0; j < c; ++j) {
        cin >> g[i][j];
        if (is_digit(g[i][j])) b.emplace_back(i, j, to_int(g[i][j]));
        if (g[i][j] == 'S') s = node(i, j, 0);
        if (g[i][j] == 'E') f = node(i, j, 0);
      }
    }
    bfs(r, c, f);
    ans = 0;
    best = dist[s.r][s.c];
    for (node i : b)
      if (dist[i.r][i.c] <= best && dist[i.r][i.c] != -1) ans += i.p;
    cout << ans << '\n';
  }
  return 0;
}
