#include <bits/stdc++.h>
using namespace std;
int n, m;
int maxn;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct node {
  int x, y;
  int step;
} f, ff;
char e[1001][1001], s[1001][1001];
void bfs(int a, int b) {
  int i, j, k, xx, yy, step, t = 0;
  e[a][b] = 'T';
  queue<node> q;
  f.x = a;
  f.y = b;
  f.step = 0;
  q.push(f);
  while (!q.empty()) {
    ff = q.front();
    q.pop();
    if (ff.step >= maxn) break;
    for (i = 0; i < 4; i++) {
      xx = ff.x + dir[i][0];
      yy = ff.y + dir[i][1];
      step = ff.step + 1;
      if (xx < 0 || xx >= n || yy < 0 || yy >= m || e[xx][yy] == 'T') continue;
      if (e[xx][yy] == 'S')
        maxn = step;
      else
        t += e[xx][yy] - '0';
      e[xx][yy] = 'T';
      f.x = xx;
      f.y = yy;
      f.step = step;
      q.push(f);
    }
  }
  printf("%d\n", t);
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    int i, j, k, x, y;
    for (i = 0; i < n; i++) scanf("%s", e[i]);
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++)
        if (e[i][j] == 'E') {
          x = i;
          y = j;
        }
    maxn = 10000000;
    bfs(x, y);
  }
}
