#include <bits/stdc++.h>
using namespace std;
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, 0};
long long int mark[1005][1005];
long long int n, m, pos;
vector<long long int> first(1000005, 0);
char arr[1005][1005];
void bfs(pair<long long int, long long int> s) {
  queue<pair<long long int, long long int> > q;
  q.push(s);
  mark[s.first][s.second] = 0;
  while (!q.empty()) {
    long long int i = q.front().first;
    long long int j = q.front().second;
    q.pop();
    for (long long int k = 0; k < (long long int)4; k++) {
      long long int x = i + dx[k];
      long long int y = j + dy[k];
      if (x < 0 || x >= n || y < 0 || y >= m) continue;
      if (arr[x][y] == 'T') continue;
      if (mark[x][y] == -1) {
        mark[x][y] = mark[i][j] + 1;
        if (arr[x][y] == 'S')
          pos = mark[x][y];
        else
          first[mark[x][y]] += (arr[x][y] - '0');
        q.push({x, y});
      }
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  pair<long long int, long long int> ax;
  for (long long int i = 0; i < (long long int)n; i++)
    for (long long int j = 0; j < (long long int)m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'E') ax = {i, j};
    }
  memset(mark, -1, sizeof mark);
  bfs(ax);
  long long int ans = 0;
  for (long long int i = 0; i < (long long int)pos + 1; i++) {
    ans += first[i];
  }
  cout << ans << "\n";
  return 0;
}
