#include <bits/stdc++.h>
using namespace std;
struct coord {
  int c, f, v;
};
coord var, pos;
int n, m, i, x, cont, h;
int pasos[1001][1001];
bool ya[1001][1001], e;
char mapa[1001][1001];
queue<coord> cola;
stack<coord> pila;
int main() {
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%s", mapa[i]);
  }
  for (i = 0; i < n; i++) {
    for (x = 0; x < m; x++) {
      if (mapa[i][x] == 'T')
        ya[i][x] = true;
      else if (mapa[i][x] == 'E') {
        ya[i][x] = true;
        var.c = i;
        var.f = x;
        var.v = 0;
        cola.push(var);
      }
    }
  }
  while (!cola.empty()) {
    pos = cola.front();
    cola.pop();
    if (pos.c - 1 >= 0 && ya[pos.c - 1][pos.f] == false) {
      pos.c--;
      cola.push(pos);
      ya[pos.c][pos.f] = true;
      pasos[pos.c][pos.f] = pasos[pos.c + 1][pos.f] + 1;
      if (mapa[pos.c][pos.f] == 'S') {
        h = pasos[pos.c][pos.f];
      }
      pos.c++;
    }
    if (pos.c + 1 < n && ya[pos.c + 1][pos.f] == false) {
      pos.c++;
      cola.push(pos);
      ya[pos.c][pos.f] = true;
      pasos[pos.c][pos.f] = pasos[pos.c - 1][pos.f] + 1;
      if (mapa[pos.c][pos.f] == 'S') {
        h = pasos[pos.c][pos.f];
      }
      pos.c--;
    }
    if (pos.f - 1 >= 0 && ya[pos.c][pos.f - 1] == false) {
      pos.f--;
      cola.push(pos);
      ya[pos.c][pos.f] = true;
      pasos[pos.c][pos.f] = pasos[pos.c][pos.f + 1] + 1;
      if (mapa[pos.c][pos.f] == 'S') {
        h = pasos[pos.c][pos.f];
      }
      pos.f++;
    }
    if (pos.f + 1 < m && ya[pos.c][pos.f + 1] == false) {
      pos.f++;
      cola.push(pos);
      ya[pos.c][pos.f] = true;
      pasos[pos.c][pos.f] = pasos[pos.c][pos.f - 1] + 1;
      if (mapa[pos.c][pos.f] == 'S') {
        h = pasos[pos.c][pos.f];
      }
      pos.f--;
    }
  }
  for (i = 0; i < n; i++) {
    for (x = 0; x < m; x++) {
      if (mapa[i][x] - '0' > 0 && mapa[i][x] - '0' < 10) {
        if (pasos[i][x] <= h && ya[i][x] == true) cont += mapa[i][x] - '0';
      }
    }
  }
  printf("%d", cont);
}
