#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
char ch[1005][1005];
bool mark[1005][1005];
int r, c, dis[1005][1005];
void bfs(int a, int b) {
  queue<pair<int, int> > q;
  mark[a][b] = true;
  dis[a][b] = 0;
  q.push(pair<int, int>(a, b));
  while (q.size() > 0) {
    pair<int, int> t;
    t = q.front();
    q.pop();
    if (mark[t.first][t.second + 1] == false &&
        ch[t.first][t.second + 1] != 'T' && t.second != c) {
      q.push(pair<int, int>(t.first, t.second + 1));
      mark[t.first][t.second + 1] = true;
      dis[t.first][t.second + 1] = dis[t.first][t.second] + 1;
    }
    if (mark[t.first][t.second - 1] == false &&
        ch[t.first][t.second - 1] != 'T' && t.second != 1) {
      q.push(pair<int, int>(t.first, t.second - 1));
      mark[t.first][t.second - 1] = true;
      dis[t.first][t.second - 1] = dis[t.first][t.second] + 1;
    }
    if (mark[t.first + 1][t.second] == false &&
        ch[t.first + 1][t.second] != 'T' && t.first != r) {
      q.push(pair<int, int>(t.first + 1, t.second));
      mark[t.first + 1][t.second] = true;
      dis[t.first + 1][t.second] = dis[t.first][t.second] + 1;
    }
    if (mark[t.first - 1][t.second] == false &&
        ch[t.first - 1][t.second] != 'T' && t.first != 1) {
      q.push(pair<int, int>(t.first - 1, t.second));
      mark[t.first - 1][t.second] = true;
      dis[t.first - 1][t.second] = dis[t.first][t.second] + 1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  pair<int, int> exit;
  pair<int, int> start;
  cin >> r >> c;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == 'E') {
        exit.first = i;
        exit.second = j;
      }
      if (ch[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  for (int i = 1; i <= r; i++)
    for (int j = 1; j <= c; j++) dis[i][j] = 1 << 30;
  bfs(exit.first, exit.second);
  int f = dis[start.first][start.second];
  int ans = 0;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      if (dis[i][j] <= f && ch[i][j] != 'E' && ch[i][j] != 'S') {
        ans += ch[i][j] - '0';
      }
    }
  }
  cout << ans;
}
