#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >,
               greater<pair<int, pair<int, int> > > >
    pq;
int r, c, i, j, ii, d[1009][1009], x, y, dis, sum = 1000000007, ans;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
char s[1009][1009];
int main() {
  cin >> r >> c;
  for (i = 0; i < r; i++) cin >> s[i];
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++)
      if (s[i][j] == 'E') {
        pq.push(make_pair(1, make_pair(i, j)));
        while (!pq.empty()) {
          dis = pq.top().first;
          x = pq.top().second.first;
          y = pq.top().second.second;
          pq.pop();
          if (x < 0 || y < 0 || x >= r || y >= c || s[x][y] == 'T' ||
              d[x][y] == 1)
            continue;
          if (dis > sum) break;
          d[x][y] = 1;
          if (s[x][y] == 'S') sum = dis, s[x][y] = '0';
          if (s[x][y] == 'E') s[x][y] = '0';
          if (dis <= sum) ans += (s[x][y] - '0');
          for (ii = 0; ii < 4; ii++)
            pq.push(make_pair(dis + 1, make_pair(x + dx[ii], y + dy[ii])));
        }
      }
  cout << ans;
}
