#include <bits/stdc++.h>
using namespace std;
const int MX = 1000 + 10;
int n, m, cnnt = 0;
char arr[MX][MX];
int dis[MX][MX];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
bool ok(int x, int y) { return (0 <= x && x < n && 0 <= y && y < m); }
pair<int, int> en, st;
void bfs() {
  queue<pair<int, int> > q;
  pair<int, int> tmp;
  int l, k;
  dis[en.first][en.second] = 0;
  tmp = en;
  q.push(tmp);
  while (!q.empty()) {
    for (int i = 0; i < 4; i++) {
      l = q.front().first + dx[i];
      k = q.front().second + dy[i];
      if (ok(l, k) && dis[l][k] == -1 && arr[l][k] != 'T') {
        dis[l][k] = dis[q.front().first][q.front().second] + 1;
        tmp.first = l;
        tmp.second = k;
        q.push(tmp);
      }
    }
    q.pop();
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dis[i][j] = -1;
      cin >> arr[i][j];
      if (arr[i][j] == 'E') en = make_pair(i, j);
      if (arr[i][j] == 'S') st = make_pair(i, j);
    }
  }
  bfs();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] != 'T' && arr[i][j] != 'E' && arr[i][j] != 'S' &&
          dis[i][j] != -1 && dis[i][j] <= dis[st.first][st.second]) {
        cnnt = cnnt + (int)(arr[i][j] - '0');
      }
    }
  }
  cout << cnnt << endl;
  return 0;
}
