#include <bits/stdc++.h>
using namespace std;
typedef struct sPoint {
  int x, y;
  sPoint() {}
  sPoint(int _x, int _y) {
    x = _x;
    y = _y;
  }
} point;
sPoint rot[] = {sPoint(1, 0), sPoint(-1, 0), sPoint(0, -1), sPoint(0, 1)};
char m[1000][1000];
bool valid(point p, int r, int c) {
  if (p.x < 0 || p.y < 0) return false;
  if (p.x >= r || p.y >= c) return false;
  if (m[p.x][p.y] == 'T') return false;
  return true;
}
vector<point> pos(point p, int r, int c) {
  vector<point> resp;
  point temp;
  for (int i = 0; i < 4; i++) {
    temp = p;
    temp.x += rot[i].x;
    temp.y += rot[i].y;
    if (valid(temp, r, c)) resp.push_back(temp);
  }
  return resp;
}
int bfs(point begin, point end, int r, int c) {
  queue<pair<point, int> > q;
  pair<point, int> atual;
  q.push(make_pair(begin, 0));
  vector<vector<bool> > visited(r, vector<bool>(c, false));
  while (!q.empty()) {
    atual = q.front();
    q.pop();
    if (visited[atual.first.x][atual.first.y]) continue;
    visited[atual.first.x][atual.first.y] = true;
    if (atual.first.x == end.x && atual.first.y == end.y) return atual.second;
    vector<point> possib = pos(atual.first, r, c);
    for (vector<point>::iterator it = possib.begin(); it != possib.end(); it++)
      q.push(make_pair(*it, atual.second + 1));
  }
  return 0x3f3f3f3f;
}
int sbfs(int steps, point begin, int r, int c) {
  queue<pair<point, int> > q;
  pair<point, int> atual;
  q.push(make_pair(begin, 0));
  vector<vector<bool> > visited(r, vector<bool>(c, false));
  int cont = 0;
  while (!q.empty()) {
    atual = q.front();
    q.pop();
    if (visited[atual.first.x][atual.first.y]) continue;
    if (atual.second > steps) continue;
    visited[atual.first.x][atual.first.y] = true;
    if (isdigit(m[atual.first.x][atual.first.y]))
      cont += m[atual.first.x][atual.first.y] - '0';
    vector<point> possib = pos(atual.first, r, c);
    for (vector<point>::iterator it = possib.begin(); it != possib.end(); it++)
      q.push(make_pair(*it, atual.second + 1));
  }
  return cont;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int r, c;
  point begin, end;
  cin >> r >> c;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      cin >> m[i][j];
      if (m[i][j] == 'S')
        begin = sPoint(i, j);
      else if (m[i][j] == 'E')
        end = sPoint(i, j);
    }
  cout << sbfs(bfs(begin, end, r, c), end, r, c) << "\n";
  return 0;
}
