#include <bits/stdc++.h>
using namespace std;
string grid[1000];
int dist[1000][1000];
bool visited[1000][1000];
long x[] = {-1, 0, 0, 1};
long y[] = {0, -1, 1, 0};
long bfs(pair<long, long> &exitt, long n, long m) {
  for (long i = 0; i < n; i++) {
    for (long j = 0; j < m; j++) {
      visited[i][j] = false;
      dist[i][j] = -1;
    }
  }
  queue<pair<pair<long, long>, long> > q;
  visited[exitt.first][exitt.second] = true;
  q.push({exitt, 0});
  dist[exitt.first][exitt.second] = 0;
  int dmax;
  while (!q.empty()) {
    pair<pair<long, long>, long> depan = q.front();
    q.pop();
    for (long i = 0; i < 4; i++) {
      long xbaru = depan.first.first + x[i];
      long ybaru = depan.first.second + y[i];
      if (xbaru >= 0 && xbaru < n && ybaru >= 0 && ybaru < m &&
          grid[xbaru][ybaru] != 'T' && !visited[xbaru][ybaru]) {
        visited[xbaru][ybaru] = true;
        dist[xbaru][ybaru] = dist[depan.first.first][depan.first.second] + 1;
        if (grid[xbaru][ybaru] == 'S') dmax = dist[xbaru][ybaru];
        q.push({{xbaru, ybaru}, depan.second + 1});
      }
    }
  }
  long sum = 0;
  for (long i = 0; i < n; i++) {
    for (long j = 0; j < m; j++) {
      if (grid[i][j] != 'T' && dist[i][j] <= dmax && dist[i][j] != -1 &&
          grid[i][j] >= '0' && grid[i][j] <= '9') {
        sum += grid[i][j] - '0';
      }
    }
  }
  return sum;
}
int main() {
  long n, m;
  cin >> n >> m;
  pair<long, long> exitt;
  for (long i = 0; i < n; i++) {
    cin >> grid[i];
    for (long j = 0; j < m; j++) {
      if (grid[i][j] == 'E') {
        exitt.first = i;
        exitt.second = j;
      }
    }
  }
  cout << bfs(exitt, n, m) << endl;
}
