#include <bits/stdc++.h>
using namespace std;
char s[1111][1111];
int d[1111][1111];
queue<pair<int, int> > Q;
vector<pair<int, int> > v;
int n, m, lim;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void check(int x, int y, int z) {
  if (x < 0 || x >= n || y < 0 || y >= m) return;
  if (s[x][y] == 'T') return;
  if (s[x][y] == 'S') lim = z;
  d[x][y] = z;
  if ('0' <= s[x][y] && s[x][y] <= '9')
    v.push_back(make_pair(s[x][y] - '0', z));
  s[x][y] = 'T';
  Q.push(make_pair(x, y));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'E') Q.push(make_pair(i, j));
  while (!Q.empty()) {
    pair<int, int> now = Q.front(), tmp;
    Q.pop();
    for (int dir = 0; dir < 4; dir++)
      check(now.first + dx[dir], now.second + dy[dir],
            d[now.first][now.second] + 1);
  }
  int ans = 0;
  for (vector<pair<int, int> >::iterator ii = v.begin(); ii != v.end(); ii++)
    if (ii->second <= lim) ans += ii->first;
  cout << ans << endl;
  return 0;
}
