#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
char Graph[1001][1001];
int ans[1001][1001];
bool visited[1101][1101];
int main() {
  int r, c, I, J;
  queue<int> qx;
  queue<int> qy;
  cin >> r >> c;
  for (long long i = 0; i < r; i++) {
    for (long long j = 0; j < c; j++) {
      cin >> Graph[i][j];
      if (Graph[i][j] == 'E') {
        I = i;
        J = j;
      }
      ans[i][j] = -1;
      visited[i][j] = false;
    }
  }
  qx.push(J);
  qy.push(I);
  visited[I][J] = true;
  ans[I][J] = 0;
  int depth, me = 0;
  while (!qx.empty()) {
    int i = qy.front();
    int j = qx.front();
    depth = ans[i][j];
    qx.pop();
    qy.pop();
    if (Graph[i][j] == 'S') {
      me = depth;
    }
    if (i > 0 && !visited[i - 1][j] && Graph[i - 1][j] != 'T') {
      i--;
      depth++;
      visited[i][j] = true;
      qx.push(j);
      qy.push(i);
      ans[i][j] = depth;
      depth--;
      i++;
    }
    if (i < r - 1 && !visited[i + 1][j] && Graph[i + 1][j] != 'T') {
      i++;
      depth++;
      visited[i][j] = true;
      qx.push(j);
      qy.push(i);
      ans[i][j] = depth;
      depth--;
      i--;
    }
    if (j > 0 && !visited[i][j - 1] && Graph[i][j - 1] != 'T') {
      j--;
      depth++;
      visited[i][j] = true;
      qx.push(j);
      qy.push(i);
      ans[i][j] = depth;
      depth--;
      j++;
    }
    if (j < c - 1 && !visited[i][j + 1] && Graph[i][j + 1] != 'T') {
      j++;
      depth++;
      visited[i][j] = true;
      qx.push(j);
      qy.push(i);
      ans[i][j] = depth;
      depth--;
      j--;
    }
  }
  int sxva = 0;
  for (long long i = 0; i < r; i++) {
    for (long long j = 0; j < c; j++) {
      if (Graph[i][j] <= '9' && Graph[i][j] >= '1' && ans[i][j] != -1 &&
          ans[i][j] <= me) {
        sxva += Graph[i][j] - '0';
      }
    }
  }
  cout << sxva;
  return 0;
}
