#include <bits/stdc++.h>
using namespace std;
const int sz = 1009;
char a[sz][sz];
bool vis[sz][sz];
vector<pair<int, int> > breeders;
int mypos;
queue<pair<pair<int, int>, int> > q;
int n, m;
int xd[] = {0, 0, 1, -1}, yd[] = {1, -1, 0, 0};
bool valid(int i, int j) { return i > -1 && j > -1 && i < n && j < m; }
void bfs() {
  while (q.size()) {
    int x = q.front().first.first, y = q.front().first.second,
        cn = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + xd[i], yy = y + yd[i];
      if (valid(xx, yy) && !vis[xx][yy] && a[xx][yy] != 'T') {
        q.push({{xx, yy}, cn + 1});
        vis[xx][yy] = 1;
        if (a[xx][yy] == 'S')
          mypos = cn + 1;
        else
          breeders.push_back({a[xx][yy] - '0', cn + 1});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'E') x = i, y = j;
    }
  q.push({{x, y}, 0});
  vis[x][y] = 1;
  bfs();
  int cn = 0;
  for (auto i : breeders)
    if (i.second <= mypos) cn += i.first;
  cout << cn;
  return 0;
}
