#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > mk;
int wl[1000 + 10][1000 + 10], a[1000 + 10][1000 + 10];
int mvs[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int ans = 0;
int n, m;
void bfs(int x, int y, int tx, int ty) {
  queue<pair<int, int> > que;
  que.push(pair<int, int>(x, y));
  bool check = true;
  while (check) {
    int size = que.size();
    for (; size > 0; size--) {
      pair<int, int> cur = que.front();
      int tmpx = cur.first, tmpy = cur.second;
      que.pop();
      for (int i = 0; i < 4; i++) {
        int nx = tmpx + mvs[i][0], ny = tmpy + mvs[i][1];
        if (nx < n && nx >= 0 && ny < m && ny >= 0 && !wl[nx][ny]) {
          wl[nx][ny] = 1;
          ans += a[nx][ny];
          que.push(pair<int, int>(nx, ny));
          if (tx == nx && ty == ny) check = false;
        }
      }
    }
  }
}
int main() {
  cin >> n >> m;
  int x, y, tx, ty;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'T') {
        wl[i][j] = 1;
      } else if (s[j] == 'S') {
        x = i;
        y = j;
      } else if (s[j] == 'E') {
        tx = i;
        ty = j;
      } else {
        a[i][j] = s[j] - '0';
      }
    }
  }
  bfs(tx, ty, x, y);
  cout << ans << endl;
  return 0;
}
