#include <bits/stdc++.h>
using namespace std;
vector<string> mp;
vector<vector<long long> > d;
int r, c;
struct point {
  long long x, y, l;
  point(long long _x, long long _y, long long _l) : x(_x), y(_y), l(_l) {}
};
void bfs(int x, int y) {
  queue<point> q;
  q.push(point(x, y, 0));
  while (!q.empty()) {
    x = q.front().x;
    y = q.front().y;
    d[x][y] = q.front().l;
    q.pop();
    if (y - 1 >= 0 && d[x][y - 1] == -1 && mp[x][y - 1] != 'T') {
      q.push(point(x, y - 1, d[x][y] + 1));
      d[x][y - 1] = d[x][y] + 1;
    }
    if (y + 1 < c && d[x][y + 1] == -1 && mp[x][y + 1] != 'T') {
      q.push(point(x, y + 1, d[x][y] + 1));
      d[x][y + 1] = d[x][y] + 1;
    }
    if (x - 1 >= 0 && d[x - 1][y] == -1 && mp[x - 1][y] != 'T') {
      q.push(point(x - 1, y, d[x][y] + 1));
      d[x - 1][y] = d[x][y] + 1;
    }
    if (x + 1 < r && d[x + 1][y] == -1 && mp[x + 1][y] != 'T') {
      q.push(point(x + 1, y, d[x][y] + 1));
      d[x + 1][y] = d[x][y] + 1;
    }
  }
}
int main() {
  cin >> r >> c;
  mp.resize(r);
  for (int i = 0; i < r; i++) cin >> mp[i];
  d.resize(r);
  for (int i = 0; i < r; i++) d[i].resize(c, -1);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
      if (mp[i][j] == 'E') bfs(i, j);
  }
  long long s = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (mp[i][j] == 'S') s = d[i][j] + 1;
  long long ans = 0;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (d[i][j] < s && d[i][j] > -1)
        if (mp[i][j] > '0' && mp[i][j] <= '9') ans += mp[i][j] - '0';
  cout << ans;
  return 0;
}
