#include <bits/stdc++.h>
using namespace std;
double eps = numeric_limits<double>::epsilon();
double PI = acos(-1);
int INF = 1e9;
long long MAX = 1e18;
int const N = 2e5 + 16;
int const M = 1e9 + 7;
int const M2 = 999999983;
int n, m;
char g[1024][1024];
int dist[1024][1024];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
pair<int, int> q[int(1e6) + 1];
void bfs(int x1, int y1) {
  int st = 0;
  int en = 0;
  q[en++] = make_pair(x1, y1);
  dist[x1][y1] = 0;
  for (; st < en; ++st) {
    int x = q[st].first;
    int y = q[st].second;
    for (int i = 0; i < 4; ++i) {
      int x2 = x + dx[i];
      int y2 = y + dy[i];
      char c = g[x2][y2];
      if (c && c != 'T' && dist[x2][y2] > dist[x][y] + 1) {
        dist[x2][y2] = dist[x][y] + 1;
        q[en++] = make_pair(x2, y2);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  pair<int, int> s, e;
  for (int i = 1; i <= n; ++i) {
    scanf(" %s", g[i] + 1);
    for (int j = 1; j <= m; ++j) {
      char c = g[i][j];
      if (c == 'E')
        e = make_pair(i, j);
      else if (c == 'S')
        s = make_pair(i, j);
      dist[i][j] = INF;
    }
  }
  bfs(e.first, e.second);
  int len = dist[s.first][s.second];
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char c = g[i][j];
      if (isdigit(c) && dist[i][j] <= len) cnt += c - '0';
    }
  printf("%d\n", cnt);
}
