#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int tt[1016][1016];
int hh[1016][1016];
int n, m;
int ex, ey;
queue<int> q;
queue<int> p;
void bfs() {
  while (!p.empty()) {
    int x = p.front();
    int y = q.front();
    p.pop();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && tt[nx][ny] == 0 &&
          !(nx == ex && ny == ey)) {
        p.push(nx);
        q.push(ny);
        tt[nx][ny] = tt[x][y] + 1;
      }
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  getchar();
  int ans = 0;
  memset(tt, 0, sizeof(tt));
  memset(hh, 0, sizeof(hh));
  char c;
  int zux, zuy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%c", &c);
      if (c >= '0' && c <= '9') {
        hh[i][j] = c - '0';
      }
      if (c == 'S') {
        zux = i;
        zuy = j;
      }
      if (c == 'E') {
        tt[i][j] = 1;
        ex = i;
        ey = j;
      }
      if (c == 'T') {
        tt[i][j] = -1;
      }
    }
    getchar();
  }
  p.push(ex);
  q.push(ey);
  bfs();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == zux && j == zuy) continue;
      if (i == ex && j == ey) continue;
      if (tt[i][j] <= tt[zux][zuy] && tt[i][j] != 0 && tt[i][j] != -1) {
        ans += hh[i][j];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
