#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
char A[N][N];
vector<pair<int, int> > v;
int dist[N][N];
int n, m;
void dfs(pair<int, int> start) {
  int x, y;
  x = start.first;
  y = start.second;
  dist[x][y] = 0;
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> s = q.front();
    q.pop();
    x = s.first;
    y = s.second;
    int d = dist[x][y];
    if ((x - 1) > 0 && dist[x - 1][y] > (d + 1) && A[x - 1][y] != 'T') {
      dist[x - 1][y] = (d + 1);
      q.push(make_pair(x - 1, y));
    }
    if ((x + 1) <= n && dist[x + 1][y] > (d + 1) && A[x + 1][y] != 'T') {
      dist[x + 1][y] = (d + 1);
      q.push(make_pair(x + 1, y));
    }
    if ((y - 1) > 0 && dist[x][y - 1] > (d + 1) && A[x][y - 1] != 'T') {
      dist[x][y - 1] = (d + 1);
      q.push(make_pair(x, y - 1));
    }
    if ((y + 1) <= m && dist[x][y + 1] > (d + 1) && A[x][y + 1] != 'T') {
      dist[x][y + 1] = (d + 1);
      q.push(make_pair(x, y + 1));
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  int i, j;
  pair<int, int> start;
  pair<int, int> des;
  for (i = 1; i <= n; i++) {
    scanf("%s", A[i] + 1);
    for (j = 1; j <= m; j++) {
      dist[i][j] = 2e9;
      if (A[i][j] == 'S') {
        start = make_pair(i, j);
      }
      if (A[i][j] == 'E') {
        des = make_pair(i, j);
      }
      if (A[i][j] > '0' && A[i][j] <= '9') {
        v.push_back(make_pair(i, j));
      }
    }
  }
  dfs(des);
  int x, y;
  x = start.first;
  y = start.second;
  int cnt = 0;
  for (i = 0; i < v.size(); i++) {
    if (dist[v[i].first][v[i].second] <= dist[x][y]) {
      cnt += (A[v[i].first][v[i].second] - '0');
    }
  }
  printf("%d\n", cnt);
}
