#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  vector<vector<char> > forest(m, vector<char>(n, 0));
  vector<vector<int> > tran(m, vector<int>(n, 0));
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  pair<int, int> end;
  pair<int, int> start;
  for (int i = 0; i < m; ++i) {
    char line[n];
    scanf("%s", line);
    for (int j = 0; j < n; ++j) {
      forest[i][j] = line[j];
      if (line[j] == 'E') {
        end.first = i;
        end.second = j;
      } else if (line[j] == 'T') {
        tran[i][j] = -1;
      } else if (line[j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  queue<pair<int, int> > steps;
  steps.push(end);
  while (steps.size()) {
    int nx = steps.front().first;
    int ny = steps.front().second;
    for (int i = 0; i < 4; ++i) {
      int nextx = nx + dx[i];
      int nexty = ny + dy[i];
      if (nextx < m and nextx >= 0 and nexty < n and nexty >= 0 and
          tran[nextx][nexty] == 0) {
        tran[nextx][nexty] = tran[nx][ny] + 1;
        pair<int, int> next;
        next.first = nextx;
        next.second = nexty;
        steps.push(next);
      }
    }
    steps.pop();
  }
  int res = 0;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      if (tran[i][j] != 0 and forest[i][j] != 'T' and forest[i][j] != 'E' and
          forest[i][j] != '0' and forest[i][j] != 'S' and
          tran[i][j] <= tran[start.first][start.second]) {
        res += forest[i][j] - '0';
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
