#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > que;
char a[1007][1007];
int b[1007][1007];
int vis[1007][1007];
int main() {
  int m, p;
  scanf("%d %d", &m, &p);
  int i, j;
  for (i = 0; i < m; i++) scanf("%s", a[i]);
  int posx, posy;
  for (i = 0; i < m; i++) {
    for (j = 0; a[i][j]; j++) {
      b[i][j] = 1e9;
      if (a[i][j] == 'E') {
        que.push(make_pair(i, j));
        b[i][j] = 0;
        vis[i][j] = 1;
      } else if (a[i][j] == 'S')
        posx = i, posy = j;
    }
  }
  int aa = 1;
  while (!que.empty()) {
    int lm = que.size();
    for (i = 0; i < lm; i++) {
      pair<int, int> tem = que.front();
      que.pop();
      if (tem.first + 1 < m && !vis[tem.first + 1][tem.second] &&
          a[tem.first + 1][tem.second] != 'T')
        que.push(make_pair(tem.first + 1, tem.second)),
            vis[tem.first + 1][tem.second] = 1,
                            b[tem.first + 1][tem.second] = aa;
      if (tem.first - 1 >= 0 && !vis[tem.first - 1][tem.second] &&
          a[tem.first - 1][tem.second] != 'T')
        que.push(make_pair(tem.first - 1, tem.second)),
            vis[tem.first - 1][tem.second] = 1,
                            b[tem.first - 1][tem.second] = aa;
      if (tem.second + 1 < p && !vis[tem.first][tem.second + 1] &&
          a[tem.first][tem.second + 1] != 'T')
        que.push(make_pair(tem.first, tem.second + 1)),
            vis[tem.first][tem.second + 1] = 1,
                                        b[tem.first][tem.second + 1] = aa;
      if (tem.second - 1 >= 0 && !vis[tem.first][tem.second - 1] &&
          a[tem.first][tem.second - 1] != 'T')
        que.push(make_pair(tem.first, tem.second - 1)),
            vis[tem.first][tem.second - 1] = 1,
                                        b[tem.first][tem.second - 1] = aa;
    }
    aa++;
  }
  int ans = 0;
  for (i = 0; i < m; i++)
    for (j = 0; j < p; j++)
      if (a[i][j] >= '0' && a[i][j] <= '9' && b[i][j] <= b[posx][posy])
        ans = ans + a[i][j] - '0';
  printf("%d\n", ans);
  return 0;
}
