#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<pair<int, int>> que;
  vector<vector<int>> dist(n, vector<int>(m, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'E') {
        que.emplace_back(i, j);
      }
    }
  }
  dist[que[0].first][que[0].second] = 0;
  for (int b = 0; b < (int)que.size(); b++) {
    auto p = que[b];
    for (int x = -1; x <= 1; x++) {
      for (int y = -1; y <= 1; y++) {
        if (x * x + y * y == 1) {
          if (p.first + x < n && p.first + x >= 0 && p.second + y < m &&
              p.second + y >= 0) {
            int kx = p.first + x, ky = p.second + y;
            if (dist[kx][ky] == -1 && a[kx][ky] != 'T') {
              dist[kx][ky] = dist[p.first][p.second] + 1;
              que.emplace_back(kx, ky);
            }
          }
        }
      }
    }
  }
  int s;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'S') {
        s = dist[i][j];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] != 'T' && a[i][j] != 'S' && a[i][j] != 'E') {
        if (dist[i][j] <= s && dist[i][j] != -1) {
          ans += (int)(a[i][j] - '0');
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
