#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long int inf = 1e17;
int r, c, x, y, start_x, start_y, ans = 0;
vector<string> srr;
vector<vector<int>> dp(1001, vector<int>(1001, -1));
vector<vector<bool>> rec(1001, vector<bool>(5001, false));
void bfs(int i, int j) {
  list<pair<int, int>> queue;
  queue.push_back(make_pair(i, j));
  dp[i][j] = 0;
  rec[i][j] = true;
  while (!queue.empty()) {
    i = queue.front().first;
    j = queue.front().second;
    queue.pop_front();
    if (i + 1 < r && srr[i + 1][j] != 'T' && !rec[i + 1][j]) {
      rec[i + 1][j] = true;
      queue.push_back(make_pair(i + 1, j));
      dp[i + 1][j] = dp[i][j] + 1;
    }
    if (i - 1 >= 0 && srr[i - 1][j] != 'T' && !rec[i - 1][j]) {
      rec[i - 1][j] = true;
      queue.push_back(make_pair(i - 1, j));
      dp[i - 1][j] = dp[i][j] + 1;
    }
    if (j - 1 >= 0 && srr[i][j - 1] != 'T' && !rec[i][j - 1]) {
      rec[i][j - 1] = true;
      queue.push_back(make_pair(i, j - 1));
      dp[i][j - 1] = dp[i][j] + 1;
    }
    if (j + 1 < c && srr[i][j + 1] != 'T' && !rec[i][j + 1]) {
      rec[i][j + 1] = true;
      queue.push_back(make_pair(i, j + 1));
      dp[i][j + 1] = dp[i][j] + 1;
    }
  }
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> r >> c;
  srr.resize(r);
  for (int i = 0; i < r; i++) {
    cin >> srr[i];
    for (int j = 0; j < c; j++) {
      if (srr[i][j] == 'E') {
        x = i;
        y = j;
      }
      if (srr[i][j] == 'S') {
        start_x = i;
        start_y = j;
      }
    }
  }
  bfs(x, y);
  int time = dp[start_x][start_y];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (dp[i][j] <= time && dp[i][j] != -1) {
        if (48 <= srr[i][j] && srr[i][j] <= 57) {
          ans += srr[i][j] - 48;
        }
      }
    }
  }
  cout << ans << endl;
}
