#include <bits/stdc++.h>
using namespace std;
int xx[] = {0, 0, -1, 1};
int yy[] = {1, -1, 0, 0};
int r, c;
char arr[1001][1001];
bool visit[1001][1001];
struct po {
  int x, y;
  int dep;
};
bool ispos(int x, int y) {
  if (x >= 0 && x < r && y >= 0 && y < c)
    return true;
  else
    return false;
}
int bfs(int x, int y) {
  visit[x][y] = true;
  int ans = 0, end = 1001 * 1001;
  memset(visit, false, sizeof(visit));
  po st;
  st.x = x;
  st.y = y;
  st.dep = 0;
  queue<po> q;
  q.push(st);
  while (!q.empty()) {
    po now = q.front();
    po nex;
    q.pop();
    for (int i = 0; i < 4; i++) {
      nex.x = now.x + xx[i];
      nex.y = now.y + yy[i];
      nex.dep = now.dep + 1;
      if (ispos(nex.x, nex.y) && !visit[nex.x][nex.y] &&
          arr[nex.x][nex.y] != 'T') {
        visit[nex.x][nex.y] = true;
        q.push(nex);
        if (arr[nex.x][nex.y] > '0' && arr[nex.x][nex.y] <= '9' &&
            nex.dep <= end)
          ans += arr[nex.x][nex.y] - '0';
        else if (arr[nex.x][nex.y] == 'S')
          end = nex.dep;
      }
    }
  }
  return ans;
}
int main() {
  while (cin >> r >> c) {
    int sx = 0, sy = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        cin >> arr[i][j];
        if (arr[i][j] == 'E') {
          sx = i;
          sy = j;
        }
      }
    }
    int ans = bfs(sx, sy);
    printf("%d\n", ans);
  }
  return 0;
}
