#include <bits/stdc++.h>
using namespace std;
inline long long minOf(long long x, long long y) { return (x < y ? x : y); }
inline long long maxOf(long long x, long long y) { return (x > y ? x : y); }
char arr[50000000], *ptr;
long long ret;
string str;
long long r, c;
int sx, sy, ex, ey;
vector<string> forest;
int distances[1001][1001];
inline long long get_int() {
  bool isNeg = false;
  ret = 0;
  while (!((*ptr >= '0' && *ptr <= '9') || *ptr == '-')) ptr++;
  while ((*ptr >= '0' && *ptr <= '9') || *ptr == '-') {
    if (*ptr == '-')
      isNeg = true;
    else
      ret = ret * 10 + (*ptr - '0');
    ptr++;
  }
  if (isNeg) ret = -ret;
  return ret;
}
inline string get_str() {
  str = "";
  while (!(*ptr == '.' || (*ptr >= 'A' && *ptr <= 'Z') ||
           (*ptr >= '0' && *ptr <= '9')))
    ptr++;
  while (*ptr == '.' || (*ptr >= 'A' && *ptr <= 'Z') ||
         (*ptr >= '0' && *ptr <= '9')) {
    str += char(*ptr);
    ptr++;
  }
  return str;
}
void traverse() {
  queue<int> xp;
  queue<int> yp;
  queue<int> dis;
  bool been[1001][1001];
  memset(been, 0, sizeof(been));
  xp.push(ex);
  yp.push(ey);
  dis.push(0);
  int cx, cy, cd;
  while (xp.size()) {
    cx = xp.front();
    cy = yp.front();
    cd = dis.front();
    xp.pop();
    yp.pop();
    dis.pop();
    if (cx < 0 || cy < 0 || cx >= r || cy >= c) continue;
    if (forest[cx][cy] == 'T') continue;
    if (been[cx][cy]) continue;
    been[cx][cy] = true;
    distances[cx][cy] = cd;
    xp.push(cx + 1);
    xp.push(cx - 1);
    xp.push(cx);
    xp.push(cx);
    yp.push(cy);
    yp.push(cy);
    yp.push(cy + 1);
    yp.push(cy - 1);
    dis.push(cd + 1);
    dis.push(cd + 1);
    dis.push(cd + 1);
    dis.push(cd + 1);
  }
}
struct point {
  int x;
  int y;
  int c;
};
int main() {
  fread(arr, sizeof(char), 50000000, stdin);
  ptr = arr;
  r = get_int();
  c = get_int();
  vector<point> breed;
  string s;
  for (int i = 0; i <= r; i++)
    for (int j = 0; j <= c; j++) distances[i][j] = 100000000;
  for (int i = 0; i < r; i++) {
    s = get_str();
    forest.push_back(s);
  }
  point tmp;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (forest[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (forest[i][j] == 'E') {
        ex = i;
        ey = j;
      }
      if (forest[i][j] >= '0' && forest[i][j] <= '9') {
        tmp.x = i;
        tmp.y = j;
        tmp.c = forest[i][j] - '0';
        breed.push_back(tmp);
      }
    }
  }
  distances[ex][ey] = 0;
  traverse();
  int myDis = distances[sx][sy];
  long long ans = 0;
  for (int i = 0; i < breed.size(); i++) {
    if (distances[breed[i].x][breed[i].y] <= myDis) {
      ans += breed[i].c;
    }
  }
  cout << ans << endl;
  return 0;
}
