#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
int dx[] = {+0, +0, +1, -1};
int dy[] = {+1, -1, +0, +0};
int n, m, vis[N][N];
string s[N];
bool ok(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }
void bfs(int x, int y) {
  queue<pair<int, int> > q1;
  q1.push(make_pair(x, y));
  while (!q1.empty()) {
    pair<int, int> r = q1.front();
    q1.pop();
    int x = r.first;
    int y = r.second;
    for (int i = 0; i < 4; i++) {
      int a = x + dx[i];
      int b = y + dy[i];
      if (ok(a, b) && s[a][b] != 'T' && vis[a][b] == 1e9) {
        vis[a][b] = vis[x][y] + 1;
        q1.push(make_pair(a, b));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> s[i];
  int x, y, a, b;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        a = i, b = j, s[i][j] = '0';
      } else if (s[i][j] == 'E') {
        x = i, y = j, s[i][j] = '0';
      }
      vis[i][j] = 1e9;
    }
  }
  vis[x][y] = 0;
  bfs(x, y);
  int now = vis[a][b], Cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'T' || s[i][j] == '0') continue;
      if (vis[i][j] <= now) Cnt += (s[i][j] - '0');
    }
  }
  cout << Cnt << "\n";
  return 0;
}
