#include <bits/stdc++.h>
using namespace std;
int n, m;
bool isvalid(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  vector<string> s(n);
  for (int i = 0; i < n; i++) cin >> s[i];
  vector<vector<int> > a(n, vector<int>(m, INT_MAX / 2));
  int x, y, w = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'E') x = i, y = j;
  map<pair<int, int>, int> M;
  queue<pair<pair<int, int>, int> > Q;
  Q.push(make_pair(make_pair(x, y), w));
  pair<pair<int, int>, int> curr;
  while (!Q.empty()) {
    curr = Q.front();
    Q.pop();
    if (M.count(curr.first)) continue;
    M[curr.first]++;
    x = curr.first.first;
    y = curr.first.second;
    w = curr.second;
    a[x][y] = w;
    if (isvalid(x + 1, y))
      if (s[x + 1][y] != 'T') Q.push(make_pair(make_pair(x + 1, y), w + 1));
    if (isvalid(x - 1, y))
      if (s[x - 1][y] != 'T') Q.push(make_pair(make_pair(x - 1, y), w + 1));
    if (isvalid(x, y + 1))
      if (s[x][y + 1] != 'T') Q.push(make_pair(make_pair(x, y + 1), w + 1));
    if (isvalid(x, y - 1))
      if (s[x][y - 1] != 'T') Q.push(make_pair(make_pair(x, y - 1), w + 1));
  }
  int ans = 0, min;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S') min = a[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] != 'S' && s[i][j] != 'T' && s[i][j] != 'E' &&
          s[i][j] != '0') {
        if (a[i][j] <= min) {
          ans += s[i][j] - 48;
        }
      }
  cout << ans << '\n';
}
