#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q;
vector<string> st;
int dis[1005][1005];
void f(int xx, int yy, int r, int c) {
  int i, j, x, y, R[] = {1, -1, 0, 0}, C[] = {0, 0, -1, 1};
  q.push(make_pair(xx, yy));
  for (i = 0; i <= r; i++) {
    for (j = 0; j <= c; j++) dis[i][j] = 1 << 30;
  }
  dis[xx][yy] = 0;
  while (!q.empty()) {
    xx = q.front().first, yy = q.front().second;
    q.pop();
    for (i = 0; i < 4; i++) {
      x = xx + R[i], y = yy + C[i];
      if (x >= 0 && x < r && y >= 0 && y < c) {
        if (st[x][y] != 'T' && dis[x][y] > dis[xx][yy] + 1) {
          dis[x][y] = dis[xx][yy] + 1;
          q.push(make_pair(x, y));
        }
      }
    }
  }
}
int main() {
  int r, c, i, j, x, y, xx, yy, ans = 0;
  scanf("%d%d", &r, &c);
  string str;
  for (i = 0; i < r; i++) {
    cin >> str;
    st.push_back(str);
  }
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      if (st[i][j] == 'S') {
        x = i, y = j;
      } else if (st[i][j] == 'E') {
        xx = i, yy = j;
      }
    }
  }
  f(xx, yy, r, c);
  x = dis[x][y];
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      if (st[i][j] >= '1' && st[i][j] <= '9')
        ans += ((y = dis[i][j]) <= x ? (st[i][j] - 48) : 0);
    }
  }
  printf("%d\n", ans);
  return 0;
}
