#include <bits/stdc++.h>
using namespace std;
char ch[1005][1005];
int num[1000010], n, m, ans = 0;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool vis[1000010];
void bfs(int x, int y) {
  queue<int> q;
  while (!q.empty()) q.pop();
  q.push(x * m + y);
  num[x * m + y] = 0;
  vis[x * m + y] = 1;
  int i, j, p, v, tmp, step = n * m + 2, xx, yy, t = 0;
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    p = tmp / m;
    v = tmp % m;
    if (num[tmp] > step) break;
    if (ch[p][v] == 'S') step = num[tmp];
    if (ch[p][v] >= '0' && ch[p][v] <= '9') ans += ch[p][v] - '0';
    int k = num[tmp];
    for (i = 0; i < 4; i++) {
      xx = p + dir[i][0];
      yy = v + dir[i][1];
      tmp = xx * m + yy;
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && ch[xx][yy] != 'T' &&
          !vis[tmp]) {
        q.push(tmp);
        vis[tmp] = 1;
        num[tmp] = k + 1;
      }
    }
  }
}
int main() {
  int i, j, k, a, b, sn, sm;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%s", ch[i]);
    for (j = 0; j < m; j++)
      if (ch[i][j] == 'E') {
        sn = i;
        sm = j;
      }
  }
  bfs(sn, sm);
  printf("%d\n", ans);
  return 0;
}
