#include <bits/stdc++.h>
using namespace std;
const long long N = 1001, INF = 1e9;
long long n, m, ans;
string s[N];
pair<long long, long long> st, en;
long long d[N][N];
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, -1, 1};
void bfs() {
  queue<pair<long long, long long> > q;
  q.push(st);
  d[st.first][st.second] = 0;
  while (!q.empty()) {
    pair<long long, long long> u = q.front();
    q.pop();
    for (long long i = 0; i < 4; ++i) {
      long long x = u.first + dx[i];
      long long y = u.second + dy[i];
      if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] == 'T') continue;
      if (d[x][y] > d[u.first][u.second] + 1) {
        d[x][y] = d[u.first][u.second] + 1;
        q.push(pair<long long, long long>(x, y));
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; ++i) {
    cin >> s[i];
    for (long long j = 0; j < m; ++j) {
      if (s[i][j] == 'S')
        en = pair<long long, long long>(i, j);
      else if (s[i][j] == 'E')
        st = pair<long long, long long>(i, j);
      d[i][j] = INF;
    }
  }
  bfs();
  for (long long i = 0; i < n; ++i)
    for (long long j = 0; j < m; ++j)
      if (pair<long long, long long>(i, j) != en &&
          d[i][j] <= d[en.first][en.second] && s[i][j] > '0' && s[i][j] <= '9')
        ans += s[i][j] - '0';
  cout << ans;
}
