#include <bits/stdc++.h>
using namespace std;
struct node {
  long long a, b, c, d;
};
bool cmp(const node &first, const node &sec) {
  if (first.a != sec.a) return first.a < sec.a;
  return first.b < sec.b;
}
bool operator<(const node &left, const node &right) { return left.b > right.b; }
int n, m, i, j, arr[1005][1005], c1[1005][1005], ans[1005][1005], r, c, sr, sc,
    dr, dc;
char str[1005];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
node x1, x2;
vector<node> v;
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    scanf("%s", str);
    for (j = 0; j < m; j++) {
      ans[i][j + 1] = 100000000;
      if (str[j] == 'S') {
        sr = i;
        sc = j + 1;
      } else if (str[j] == 'E') {
        dr = i;
        dc = j + 1;
      } else if (str[j] == 'T')
        arr[i][j + 1] = -1;
      else if (str[j] > '0') {
        x1.a = i;
        x1.b = j + 1;
        x1.c = str[j] - '0';
        v.push_back(x1);
      }
    }
  }
  c1[dr][dc] = 1;
  ans[dr][dc] = 0;
  x1.a = dr;
  x1.b = dc;
  queue<node> q;
  q.push(x1);
  while (!q.empty()) {
    x1 = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      r = x1.a + dx[i];
      c = x1.b + dy[i];
      if ((r > 0) && (r <= n) && (c > 0) && (c <= m) && (c1[r][c] == 0) &&
          (arr[r][c] != -1)) {
        c1[r][c] = 1;
        ans[r][c] = ans[x1.a][x1.b] + 1;
        node x2;
        x2.a = r;
        x2.b = c;
        q.push(x2);
      }
    }
  }
  int mind = ans[sr][sc];
  int breeders = 0;
  for (i = 0; i < v.size(); i++) {
    x1 = v[i];
    if (ans[x1.a][x1.b] <= mind) breeders += x1.c;
  }
  cout << breeders;
  return 0;
}
