#include <bits/stdc++.h>
using namespace std;
int r, c;
int xchan[] = {1, -1, 0, 0};
int ychan[] = {0, 0, -1, 1};
char in[1009][1009];
struct z {
  int a, b;
};
z st, ed;
vector<z> vc;
int vis[1009][1009];
int cost[1009][1009];
int dp[1009][1009];
void BFS() {
  z sc = ed;
  queue<z> q;
  q.push(sc);
  vis[sc.a][sc.b] = 1;
  cost[sc.a][sc.b] = 1;
  while (q.size()) {
    z u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      z v;
      v.a = u.a + xchan[i];
      v.b = u.b + ychan[i];
      if (vis[v.a][v.b] == 0 && (v.a <= r && v.a > 0 && v.b > 0 && v.b <= c) &&
          in[v.a][v.b] != 'T') {
        vis[v.a][v.b] = 1;
        cost[v.a][v.b] = cost[u.a][u.b] + 1;
        q.push(v);
      }
    }
  }
}
int main() {
  cin >> r >> c;
  getchar();
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> in[i][j];
      if (in[i][j] == 'S') {
        st.a = i;
        st.b = j;
      }
      if (in[i][j] == 'E') {
        ed.a = i;
        ed.b = j;
      }
      if (in[i][j] <= '9' && in[i][j] > '0') {
        z tmp;
        tmp.a = i;
        tmp.b = j;
        vc.push_back(tmp);
      }
    }
    getchar();
  }
  BFS();
  int me = cost[st.a][st.b];
  int len = vc.size();
  int ans = 0;
  for (int i = 0; i < len; i++) {
    if (cost[vc[i].a][vc[i].b] <= me && cost[vc[i].a][vc[i].b] != 0)
      ans += in[vc[i].a][vc[i].b] - '0';
  }
  cout << ans << endl;
  return 0;
}
