#include <bits/stdc++.h>
const int MAX_N = 102;
using namespace std;
template <typename _Tp, typename _Alloc, typename _Up>
inline typename vector<_Tp, _Alloc>::size_type erase(
    vector<_Tp, _Alloc>& __cont, const _Up& __value) {
  const auto __osz = __cont.size();
  __cont.erase(std::remove(__cont.begin(), __cont.end(), __value),
               __cont.end());
  return __osz - __cont.size();
}
struct cell {
  int x, y, dis;
};
int distanceMatrix[1002][1002];
int sX, sY;
int n, m;
void findShortestPath(int i, int j, char** a) {
  queue<cell> q;
  cell temp;
  temp.x = i, temp.y = j, temp.dis = 0;
  q.push(temp);
  bool visited[n + 10][m + 10];
  memset(visited, false, sizeof(visited));
  visited[i][j] = true;
  distanceMatrix[i][j] = 0;
  while (!q.empty()) {
    cell primary = q.front();
    q.pop();
    if (primary.x + 1 < n && !visited[primary.x + 1][primary.y] &&
        a[primary.x + 1][primary.y] != 'T') {
      visited[primary.x + 1][primary.y] = true;
      temp.x = primary.x + 1;
      temp.y = primary.y;
      temp.dis = primary.dis + 1;
      distanceMatrix[temp.x][temp.y] = temp.dis;
      q.push(temp);
    }
    if (primary.x - 1 >= 0 && !visited[primary.x - 1][primary.y] &&
        a[primary.x - 1][primary.y] != 'T') {
      visited[primary.x - 1][primary.y] = true;
      temp.x = primary.x - 1;
      temp.y = primary.y;
      temp.dis = primary.dis + 1;
      distanceMatrix[temp.x][temp.y] = temp.dis;
      q.push(temp);
    }
    if (primary.y + 1 < m && !visited[primary.x][primary.y + 1] &&
        a[primary.x][primary.y + 1] != 'T') {
      visited[primary.x][primary.y + 1] = true;
      temp.x = primary.x;
      temp.y = primary.y + 1;
      temp.dis = primary.dis + 1;
      distanceMatrix[temp.x][temp.y] = temp.dis;
      q.push(temp);
    }
    if (primary.y - 1 >= 0 && !visited[primary.x][primary.y - 1] &&
        a[primary.x][primary.y - 1] != 'T') {
      visited[primary.x][primary.y - 1] = true;
      temp.x = primary.x;
      temp.y = primary.y - 1;
      temp.dis = primary.dis + 1;
      distanceMatrix[temp.x][temp.y] = temp.dis;
      q.push(temp);
    }
  }
  return;
}
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  };
  for (int i = 0; i < 1000; i++)
    for (int j = 0; j < 1000; j++) distanceMatrix[i][j] = 99999999;
  cin >> n >> m;
  char** a = new char*[n];
  for (int i = 0; i < n; i++) {
    a[i] = new char[m];
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sX = i, sY = j;
    }
  }
  int shDisMa;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'E') {
        findShortestPath(i, j, a);
        distanceMatrix[i][j] = 99999999;
      }
    }
  }
  shDisMa = distanceMatrix[sX][sY];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '1' || a[i][j] == '2' || a[i][j] == '3' ||
          a[i][j] == '4' || a[i][j] == '5' || a[i][j] == '6' ||
          a[i][j] == '7' || a[i][j] == '8' || a[i][j] == '9') {
        if (distanceMatrix[i][j] <= shDisMa) {
          ans += a[i][j] - '0';
        }
      }
    }
  }
  cout << ans;
  return 0;
}
