#include <bits/stdc++.h>
using namespace std;
int main() {
  int r, c, sx, sy, ex, ey, ans = 0;
  cin >> r >> c;
  char arr[r][c];
  int cnt[r][c], vis[r][c];
  int flag = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cnt[i][j] = vis[i][j] = 0;
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        sx = i;
        sy = j;
      } else if (arr[i][j] == 'E') {
        ex = i;
        ey = j;
      }
    }
  }
  queue<pair<int, int> > Q;
  Q.push(make_pair(ex, ey));
  vis[ex][ey] = 1;
  while (!Q.empty()) {
    int tempx, tempy;
    pair<int, int> a;
    a = Q.front();
    tempx = a.first;
    tempy = a.second;
    Q.pop();
    if (flag) {
      if (cnt[tempx][tempy] == flag) break;
    }
    if (tempx - 1 >= 0) {
      if (vis[tempx - 1][tempy] != 1 && arr[tempx - 1][tempy] != 'T') {
        if (arr[tempx - 1][tempy] != 'S') {
          ans += ((arr[tempx - 1][tempy] - 48));
        }
        cnt[tempx - 1][tempy] = cnt[tempx][tempy] + 1;
        vis[tempx - 1][tempy] = 1;
        if (!flag) Q.push(make_pair(tempx - 1, tempy));
      }
    }
    if (tempy - 1 >= 0) {
      if (vis[tempx][tempy - 1] != 1 && arr[tempx][tempy - 1] != 'T') {
        if (arr[tempx][tempy - 1] != 'S') {
          ans += ((arr[tempx][tempy - 1] - 48));
        }
        cnt[tempx][tempy - 1] = cnt[tempx][tempy] + 1;
        vis[tempx][tempy - 1] = 1;
        if (!flag) Q.push(make_pair(tempx, tempy - 1));
      }
    }
    if (tempx + 1 < r) {
      if (vis[tempx + 1][tempy] != 1 && arr[tempx + 1][tempy] != 'T') {
        if (arr[tempx + 1][tempy] != 'S') {
          ans += ((arr[tempx + 1][tempy] - 48));
        }
        cnt[tempx + 1][tempy] = cnt[tempx][tempy] + 1;
        vis[tempx + 1][tempy] = 1;
        if (!flag) Q.push(make_pair(tempx + 1, tempy));
      }
    }
    if (tempy + 1 < c) {
      if (vis[tempx][tempy + 1] != 1 && arr[tempx][tempy + 1] != 'T') {
        if (arr[tempx][tempy + 1] != 'S') {
          ans += ((arr[tempx][tempy + 1]) - 48);
        }
        cnt[tempx][tempy + 1] = cnt[tempx][tempy] + 1;
        vis[tempx][tempy + 1] = 1;
        if (!flag) Q.push(make_pair(tempx, tempy + 1));
      }
    }
    if ((tempx - 1 == sx && tempy == sy) || (tempx + 1 == sx && tempy == sy) ||
        (tempx == sx && tempy - 1 == sy) || (tempx == sx && tempy + 1 == sy))
      flag = cnt[tempx][tempy] + 1;
  }
  cout << ans;
}
