#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
vector<string> v;
bool vis[1002][1002];
int n, m;
int dis[1002][1002];
void bfs(int r, int c) {
  vis[r][c] = true;
  queue<pair<int, int> > q;
  q.push({r, c});
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = dx[i] + p.first, y = dy[i] + p.second;
      if (x >= 0 and y >= 0 and x < n and y < m and !vis[x][y] and
          v[x][y] != 'T') {
        dis[x][y] = dis[p.first][p.second] + 1;
        q.push({x, y});
        vis[x][y] = true;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  int e1, e2, s1, s2;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    v.push_back(s);
    for (int j = 0; j < m; j++) {
      if (v[i][j] == 'S')
        s1 = i, s2 = j;
      else if (v[i][j] == 'E')
        e1 = i, e2 = j;
    }
  }
  dis[e1][e2] = 0;
  bfs(e1, e2);
  int ans = 0;
  int ch = dis[s1][s2];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (v[i][j] >= '1' and v[i][j] <= '9' and dis[i][j] <= ch and vis[i][j])
        ans += v[i][j] - '0';
    }
  }
  cout << ans << endl;
  return 0;
}
