#include <bits/stdc++.h>
using namespace std;
char mp[1001][1001];
int v[1001][1001];
int v1[1001][1001];
int dist;
int n, m;
int l(int x, int y) {
  queue<pair<pair<int, int>, int> > q;
  q.push({{x, y}, 0});
  while (!q.empty()) {
    int xs = q.front().first.first;
    int ys = q.front().first.second;
    int ls = q.front().second;
    if (xs >= n || ys >= m || xs < 0 || ys < 0) {
      q.pop();
      continue;
    }
    if (v[xs][ys]) {
      q.pop();
      continue;
    }
    v[xs][ys] = 1;
    if (mp[xs][ys] == 'T') {
      q.pop();
      continue;
    }
    if (mp[xs][ys] == 'E') {
      return q.front().second;
      break;
    }
    q.push({{xs + 1, ys}, ls + 1});
    q.push({{xs - 1, ys}, ls + 1});
    q.push({{xs, ys + 1}, ls + 1});
    q.push({{xs, ys - 1}, ls + 1});
  }
  return 0;
}
int d(int x, int y) {
  int ans = 0;
  queue<pair<pair<int, int>, int> > q;
  q.push({{x, y}, 0});
  while (!q.empty()) {
    int xs = q.front().first.first;
    int ys = q.front().first.second;
    int ls = q.front().second;
    if (xs >= n || ys >= m || xs < 0 || ys < 0) {
      q.pop();
      continue;
    }
    if (v1[xs][ys]) {
      q.pop();
      continue;
    }
    v1[xs][ys] = 1;
    if (mp[xs][ys] == 'T') {
      q.pop();
      continue;
    }
    if (mp[xs][ys] != 'E' && mp[xs][ys] != 'S' && ls <= dist) {
      ans += mp[xs][ys] - '0';
    }
    if (ls > dist) {
      return ans;
    }
    q.push({{xs + 1, ys}, ls + 1});
    q.push({{xs - 1, ys}, ls + 1});
    q.push({{xs, ys + 1}, ls + 1});
    q.push({{xs, ys - 1}, ls + 1});
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  int xe, ye;
  int xs, ys;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') {
        xs = i;
        ys = j;
      }
      if (mp[i][j] == 'E') {
        xe = i;
        ye = j;
      }
    }
  }
  dist = l(xs, ys);
  cout << d(xe, ye) << endl;
  return 0;
}
