#include <bits/stdc++.h>
using namespace std;
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
int n;
int m;
int a[1001][1001];
char buf[1001];
int dist[1001][1001];
bool use[1001][1001];
bool ok(int aa, int b) {
  if (aa < 0 || b < 0 || aa >= n || b >= m) {
    return false;
  }
  if (use[aa][b]) {
    return false;
  }
  if (a[aa][b] == -1) {
    return false;
  }
  return true;
}
bool Ok(int aa, int b) {
  if (aa < 0 || b < 0 || aa >= n || b >= m) {
    return false;
  }
  if (a[aa][b] == -1) {
    return false;
  }
  return true;
}
int ss;
int sss;
int tt;
int ttt;
int dist2[1001][1001];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", buf);
    for (int j = 0; j < m; j++) {
      if (buf[j] == 'S') {
        ss = i;
        sss = j;
        use[i][j] = true;
        continue;
      }
      if (buf[j] == 'T') {
        a[i][j] = -1;
        continue;
      }
      if (buf[j] == 'E') {
        tt = i;
        ttt = j;
        a[i][j] = INT_MAX;
        continue;
      }
      a[i][j] = buf[j] - '0';
    }
  }
  {
    queue<pair<int, int> > q;
    q.push(make_pair(ss, sss));
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      int nex = dist[x][y] + 1;
      if (a[x][y] == INT_MAX) {
        break;
      }
      for (int i = 0; i < 4; i++) {
        int xx = x + X[i];
        int yy = y + Y[i];
        if (ok(xx, yy)) {
          dist[xx][yy] = nex;
          use[xx][yy] = true;
          q.push(make_pair(xx, yy));
        }
      }
    }
  }
  {
    queue<pair<int, int> > q;
    q.push(make_pair(tt, ttt));
    memset(use, false, sizeof(use));
    memset(dist2, -1, sizeof(dist2));
    int countt = 0;
    use[tt][ttt] = true;
    dist2[tt][ttt] = dist[tt][ttt];
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      if (x != tt || y != ttt) {
        countt += a[x][y];
      }
      q.pop();
      int dis = dist2[x][y];
      if (dis == 0) {
        continue;
      }
      dis--;
      for (int i = 0; i < 4; i++) {
        int xx = x + X[i];
        int yy = y + Y[i];
        if (ok(xx, yy)) {
          use[xx][yy] = true;
          dist2[xx][yy] = dis;
          q.push(make_pair(xx, yy));
        }
      }
    }
    printf("%d\n", countt);
  }
  return 0;
}
