#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long modpow(long long a, long long n) {
  long long ret = 1;
  long long b = a;
  while (n) {
    if (n & 1) ret = (ret * b) % 1000000007ll;
    b = (b * b) % 1000000007ll;
    n >>= 1;
  }
  return (long long)ret;
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
char arr[1005][1005];
int vis[1005][1005];
int dist[1005][1005];
bool isvalid(int x, int y) {
  if (x >= 0 and y >= 0 and x < n and y < m and !vis[x][y] and arr[x][y] != 'T')
    return true;
  return false;
}
pair<int, int> st, en;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S')
        st = make_pair(i, j);
      else if (arr[i][j] == 'E')
        en = make_pair(i, j);
    }
  }
  memset(dist, 9999999, sizeof(dist));
  queue<pair<pair<int, int>, int> > qu;
  qu.push(make_pair(en, 0));
  dist[en.first][en.second] = 0;
  vis[en.first][en.second] = 1;
  while (!qu.empty()) {
    auto x = qu.front();
    qu.pop();
    vis[x.first.first][x.first.second] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = x.first.first + dx[i];
      int ny = x.first.second + dy[i];
      if (isvalid(nx, ny)) {
        dist[nx][ny] =
            min(dist[x.first.first][x.first.second] + 1, dist[nx][ny]);
        qu.push(make_pair(make_pair(nx, ny), dist[nx][ny]));
        vis[nx][ny] = 1;
      }
    }
  }
  int mydist = dist[st.first][st.second];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] >= '0' and arr[i][j] <= '9' and dist[i][j] <= mydist)
        ans += arr[i][j] - '0';
    }
  }
  cout << ans;
  return 0;
}
