#include <bits/stdc++.h>
using namespace std;
int tu(int val) { return (1 << val); }
bool iset(int mask, int id) {
  if ((mask & tu(id)) != 0) return true;
  return false;
}
void doset(int &mask, int id) { mask |= tu(id); }
void dounset(int &mask, int id) { mask = mask & (~tu(id)); }
template <typename T>
string tos(T a) {
  stringstream ss;
  string ret;
  ss << a;
  ss >> ret;
  return ret;
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r, c, depth[1009][1009];
char grid[1009][1009], color[1009][1009];
int valid(int x, int y) {
  if (x < 0 || y < 0 || x >= r || y >= c) return 0;
  return 1;
}
int dist[1000100];
int main() {
  while (cin >> r >> c) {
    memset(dist, 0, sizeof dist);
    ;
    memset(color, 0, sizeof color);
    ;
    int md;
    for (int(i) = (0); (i) < (r); (i)++) {
      scanf("%s", &grid[i]);
    }
    int va, vb;
    for (int(i) = (0); (i) < (r); (i)++)
      for (int(j) = (0); (j) < (c); (j)++) {
        if (grid[i][j] == 'E') {
          va = i;
          vb = j;
          j = c;
          i = r;
          break;
        }
      }
    queue<int> qa, qb;
    qa.push(va);
    qb.push(vb);
    depth[va][vb] = 0;
    color[va][vb] = 1;
    while (qa.empty() == false) {
      int ua = qa.front();
      int ub = qb.front();
      qa.pop();
      qb.pop();
      int na, nb;
      for (int(k) = (0); (k) < (4); (k)++) {
        na = ua + dx[k];
        nb = ub + dy[k];
        if (valid(na, nb) && color[na][nb] == 0 && grid[na][nb] != 'T') {
          color[na][nb] = 1;
          qa.push(na);
          qb.push(nb);
          depth[na][nb] = depth[ua][ub] + 1;
          if (grid[na][nb] == 'S')
            md = depth[na][nb];
          else if (grid[na][nb] >= '0' && grid[na][nb] <= '9')
            dist[depth[na][nb]] += (grid[na][nb] - '0');
        }
      }
    }
    int ret = 0;
    for (int(i) = (0); (i) < (md + 1); (i)++) {
      ret += dist[i];
    }
    cout << ret << endl;
  }
  return 0;
}
