#include <bits/stdc++.h>
using namespace std;
queue<int> myque;
const int INF = 1000000;
vector<int> lie;
int n, m;
vector<int> par[1010000];
void bfs(int src) {
  for (int i = 0; i < n * m; i++) lie.push_back(INF);
  int node;
  myque.push(src);
  lie[src] = 0;
  while (!myque.empty()) {
    node = myque.front();
    myque.pop();
    int ad = lie[node];
    for (int i = 0; i < par[node].size(); i++) {
      int v = par[node][i];
      if (lie[v] > ad + 1) {
        lie[v] = ad + 1;
        myque.push(v);
      }
    }
  }
}
string lab[1100];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> lab[i];
  int sal = 0, me = 0;
  vector<pair<int, int> > mekons;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c = lab[i][j];
      int id = i * m + j;
      if (lab[i][j] == 'E') sal = id;
      if (lab[i][j] != 'T' && lab[i][j] != 'S' && lab[i][j] != '0' &&
          lab[i][j] != 'E')
        mekons.push_back(make_pair(id, int(c - '0')));
      if (lab[i][j] == 'S') me = id;
      if (i + 1 < n) {
        if (lab[i][j] != 'T' && lab[i + 1][j] != 'T') {
          par[id].push_back(id + m);
          par[id + m].push_back(id);
        }
      }
      if (j + 1 < m) {
        if (lab[i][j] != 'T' && lab[i][j + 1] != 'T') {
          par[id].push_back(id + 1);
          par[id + 1].push_back(id);
        }
      }
    }
  }
  bfs(sal);
  int yo = lie[me];
  int cnt = 0;
  for (int i = 0; i < mekons.size(); i++) {
    if (lie[mekons[i].first] <= yo) cnt += mekons[i].second;
  }
  cout << cnt << endl;
}
