#include <bits/stdc++.h>
using namespace std;
string mat[1002];
int dis[1002][1002];
int r, c;
void initialize() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> mat[i];
}
void reset() {
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) dis[i][j] = -1;
  }
}
int calc(int pos_i = -1, int pos_j = -1, bool shortest = 0, int min_dis = 1e9) {
  reset();
  queue<pair<int, int> > q;
  q.push(make_pair(pos_i, pos_j));
  dis[pos_i][pos_j] = 0;
  int mons = 0;
  while (!q.empty()) {
    int cur_i = q.front().first;
    int cur_j = q.front().second;
    int cur_dis = dis[cur_i][cur_j];
    if (shortest && mat[cur_i][cur_j] == 'E')
      return cur_dis;
    else if (!shortest && isdigit(mat[cur_i][cur_j]) && cur_dis <= min_dis) {
      mons += mat[cur_i][cur_j] - '0';
    }
    if (cur_i + 1 < r && mat[cur_i + 1][cur_j] != 'T') {
      if (dis[cur_i + 1][cur_j] == -1) {
        dis[cur_i + 1][cur_j] = 1 + cur_dis;
        q.push(make_pair(cur_i + 1, cur_j));
      }
    }
    if (cur_j + 1 < c && mat[cur_i][cur_j + 1] != 'T') {
      if (dis[cur_i][cur_j + 1] == -1) {
        dis[cur_i][cur_j + 1] = 1 + cur_dis;
        q.push(make_pair(cur_i, cur_j + 1));
      }
    }
    if (cur_i - 1 >= 0 && mat[cur_i - 1][cur_j] != 'T') {
      if (dis[cur_i - 1][cur_j] == -1) {
        dis[cur_i - 1][cur_j] = 1 + cur_dis;
        q.push(make_pair(cur_i - 1, cur_j));
      }
    }
    if (cur_j - 1 >= 0 && mat[cur_i][cur_j - 1] != 'T') {
      if (dis[cur_i][cur_j - 1] == -1) {
        dis[cur_i][cur_j - 1] = 1 + cur_dis;
        q.push(make_pair(cur_i, cur_j - 1));
      }
    }
    q.pop();
  }
  if (shortest) return 1e9;
  return mons;
}
int compute() {
  int shortest = 0, mons = 0, ei, ej;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (mat[i][j] == 'S') {
        shortest = calc(i, j, 1);
      }
      if (mat[i][j] == 'E') {
        ei = i;
        ej = j;
      }
    }
  }
  return calc(ei, ej, 0, shortest);
}
int main() {
  initialize();
  cout << compute();
  return 0;
}
