#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001;
char a[maxn][maxn];
bool bio[maxn][maxn];
int d[maxn][maxn];
int r, c;
pair<int, int> s, e;
int smjx[] = {0, 1, -1, 0};
int smjy[] = {1, 0, 0, -1};
int sol = 0;
bool uvjet(int x, int y) {
  if (x >= r || x < 0 || y >= c || y < 0 || bio[x][y]) return 0;
  return !(a[x][y] == 'T');
}
void bfs(pair<int, int> start) {
  bio[start.first][start.second] = 1;
  queue<pair<int, int> > q;
  q.push(start);
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++)
      if (uvjet(u.first + smjx[i], u.second + smjy[i])) {
        q.push(make_pair(u.first + smjx[i], u.second + smjy[i]));
        d[u.first + smjx[i]][u.second + smjy[i]] = d[u.first][u.second] + 1;
        bio[u.first + smjx[i]][u.second + smjy[i]] = 1;
      }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> r >> c;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'E') {
        e.first = i;
        e.second = j;
      }
      if (a[i][j] == 'S') {
        s.first = i;
        s.second = j;
      }
    }
  bfs(e);
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      if (a[i][j] != '0' && a[i][j] != 'T' && a[i][j] != 'E' &&
          a[i][j] != 'S' && d[i][j] <= d[s.first][s.second] && d[i][j])
        sol += (a[i][j] - '0');
    }
  cout << sol;
  return 0;
}
