#include <bits/stdc++.h>
using namespace std;
int R, C, start, person;
vector<pair<int, int> > con[1000009];
int ds[1000009];
char Map[1003][1003];
vector<pair<int, int> > vert;
void Read() {
  scanf("%d %d", &R, &C);
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cin >> Map[i][j];
      if (Map[i][j] >= '0' && Map[i][j] <= '9')
        vert.push_back(make_pair(i * C + j, Map[i][j] - '0'));
      if (Map[i][j] == 'E') start = i * C + j;
      if (Map[i][j] == 'S') {
        person = i * C + j;
      }
    }
  }
}
void Make_Graph() {
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      if (j > 0 && Map[i][j - 1] != 'T')
        con[i * C + j].push_back(make_pair(i * C + j - 1, 1));
      if (j < C - 1 && Map[i][j + 1] != 'T')
        con[i * C + j].push_back(make_pair(i * C + j + 1, 1));
      if (i > 0 && Map[i - 1][j] != 'T')
        con[i * C + j].push_back(make_pair((i - 1) * C + j, 1));
      if (i < R - 1 && Map[i + 1][j] != 'T')
        con[i * C + j].push_back(make_pair((i + 1) * C + j, 1));
    }
  }
}
void Dijkstra() {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      Q;
  for (int i = 0; i <= R * C + 1; i++) ds[i] = 100000009;
  ds[start] = 0;
  Q.push(make_pair(0, start));
  while (!Q.empty()) {
    pair<int, int> p = Q.top();
    Q.pop();
    int here = p.second;
    for (int i = 0; i < con[here].size(); i++) {
      if (ds[here] + con[here][i].second < ds[con[here][i].first]) {
        ds[con[here][i].first] = ds[here] + con[here][i].second;
        Q.push(make_pair(ds[con[here][i].first], con[here][i].first));
      }
    }
  }
}
void Solve() {
  Dijkstra();
  int decide = ds[person];
  int ans = 0;
  for (int i = 0; i < vert.size(); i++) {
    if (ds[vert[i].first] <= decide) ans += vert[i].second;
  }
  printf("%d\n", ans);
}
int main() {
  Read();
  Make_Graph();
  Solve();
  return 0;
}
