#include <bits/stdc++.h>
using namespace std;
char ma[1005][1005];
int vis[1005][1005];
int ans[1005][1005];
int sum, n, m;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct node {
  int x;
  int y;
};
void bfs(int x, int y) {
  queue<node> q;
  node s;
  s.x = x;
  s.y = y;
  vis[x][y] = 1;
  q.push(s);
  while (!q.empty()) {
    s = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      node t = s;
      t.x += dx[i];
      t.y += dy[i];
      if (t.x >= 0 && t.y >= 0 && t.x < n && t.y < m && vis[t.x][t.y] == 0 &&
          ma[t.x][t.y] != 'T') {
        vis[t.x][t.y] = 1;
        for (int j = 0; j < 4; j++) {
          node f = t;
          f.x += dx[j];
          f.y += dy[j];
          if (f.x >= 0 && f.y >= 0 && f.x < n && f.y < m &&
              vis[f.x][f.y] == 1 && ma[f.x][f.y] != 'T') {
            if (ans[t.x][t.y] == 0)
              ans[t.x][t.y] = ans[f.x][f.y] + 1;
            else
              ans[t.x][t.y] = min(ans[t.x][t.y], ans[f.x][f.y] + 1);
          };
        }
        q.push(t);
      }
    }
  }
}
int main() {
  int x1, y1, x2, y2, sum = 0;
  scanf("%d%d", &n, &m);
  getchar();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%c", &ma[i][j]);
      if (ma[i][j] == 'E') {
        x1 = i, y1 = j;
      }
      if (ma[i][j] == 'S') {
        x2 = i, y2 = j;
      }
    }
    getchar();
  }
  bfs(x1, y1);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (ans[i][j] != 0 && (ans[i][j] <= ans[x2][y2]) && ma[i][j] != 'T')
        sum += ma[i][j] - '0';
    }
  cout << sum + '0' - 'S' << endl;
}
