#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
bool b[1005][1005], f = 1;
int i, j, r, c, ei, ej, C, k, ii, ij, is, n = -1;
int main() {
  list<int> I;
  list<int> J;
  list<int> S;
  cin >> r >> c;
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'E') {
        I.push_back(i);
        J.push_back(j);
        S.push_back(0);
        b[i][j] = 1;
      }
    }
  }
  for (i = 0; i <= r + 1; i++) {
    a[i][0] = 'T';
    a[i][c + 1] = 'T';
  }
  for (i = 1; i <= c; i++) {
    a[0][i] = 'T';
    a[r + 1][i] = 'T';
  }
  while (!I.empty()) {
    ii = I.front();
    ij = J.front();
    is = S.front();
    I.pop_front();
    J.pop_front();
    S.pop_front();
    if (is == n) break;
    for (k = 0; k < 4; k++) {
      switch (k) {
        case 0:
          i = ii;
          j = ij + 1;
          break;
        case 1:
          i = ii + 1;
          j = ij;
          break;
        case 2:
          i = ii;
          j = ij - 1;
          break;
        case 3:
          i = ii - 1;
          j = ij;
          break;
      }
      if (a[i][j] != 'T' && !b[i][j]) {
        b[i][j] = 1;
        if (a[i][j] == 'S')
          n = is + 1;
        else
          C += a[i][j] - 48;
        I.push_back(i);
        J.push_back(j);
        S.push_back(is + 1);
      }
    }
  }
  cout << C;
}
