#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
void _print(int x) { cerr << x; }
void _print(float x) { cerr << x; }
void _print(string x) { cerr << x; }
void _print(ll x) { cerr << x; }
void _print(char x) { cerr << x; }
void _print(bool x) { cerr << x; }
template <class T, class V>
void _print(pair<T, V> x) {
  cerr << "[";
  _print(x.first);
  cerr << " ";
  _print(x.second);
  cerr << "]";
}
template <class T>
void _print(vector<T> x) {
  cerr << "[ ";
  for (T i : x) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(set<T> x) {
  cerr << "[ ";
  for (T i : x) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& i : v) is >> i;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U> p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, vector<pair<T, U>>& p) {
  for (auto& i : p) {
    is >> i.first >> i.second;
  }
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, vector<pair<T, U>>& p) {
  for (auto& i : p) {
    os << i.first << " " << i.second << endl;
  }
  return os;
}
template <typename T>
pair<T, bool> getNthElement(set<T>& searchSet, int index) {
  pair<T, bool> result;
  if (searchSet.size() > index) {
    result.first = *(std::next(searchSet.begin(), index));
    result.second = true;
  } else
    result.second = false;
  return result;
}
template <typename T>
istream& operator>>(istream& is, set<T>& v) {
  for (auto& i : v) is >> i;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
const ll mod = 1e9 + 7;
const int N = 1e3 + 10;
template <typename T, typename U>
ll modMul(T a, U b) {
  return ((a % mod) * 1LL * (b % mod)) % mod;
}
template <typename T, typename U>
ll binpower(T n, U k) {
  T res = 1;
  while (k) {
    if (k & 1) {
      res = modMul(res, n);
    }
    n = modMul(n, n);
    k >>= 1;
  }
  return res % mod;
}
template <typename T>
T moduloInverse(T a) {
  return binpower(a, mod - 2);
}
template <typename T, typename U>
ll modAdd(T a, U b) {
  return ((a % mod) + (b % mod)) % mod;
}
template <typename T, typename U>
ll modSub(T a, U b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
template <typename T, typename U>
ll modDiv(T a, U b) {
  return modMul(a, moduloInverse(b));
}
int bfsN = N, bfsM = N;
int vis[N][N], dist[N][N];
vector<string> arr;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
bool bfsIsValid(int x, int y) {
  if (x < 0 || x >= bfsN || y < 0 || y >= bfsM) return false;
  if (vis[x][y] == 1 || vis[x][y] == -1) return false;
  return true;
}
int BFS(int srcX, int srcY) {
  queue<pair<int, int>> q;
  int ans = 0;
  int d = INT_MAX;
  for (int i = 0; i < bfsN; ++i) {
    for (int j = 0; j < bfsM; ++j) {
      vis[i][j] = 0, dist[i][j] = 0;
      if (arr[i][j] == 'T') {
        vis[i][j] = -1;
        dist[i][j] = INT_MAX;
      }
      if (arr[i][j] == 'E') srcX = i, srcY = j;
    }
  }
  q.push(make_pair(srcX, srcY));
  dist[srcX][srcY] = 0;
  while (!q.empty()) {
    auto k = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int a = k.first + dx[i], b = k.second + dy[i];
      if (bfsIsValid(a, b)) {
        vis[a][b] = 1;
        q.push(make_pair(a, b));
        dist[a][b] = dist[k.first][k.second] + 1;
        if (arr[a][b] == 'S') {
          d = dist[a][b];
        }
        if (dist[a][b] <= d && arr[a][b] > '0' && arr[a][b] <= '9')
          ans += arr[a][b] - '0';
      }
    }
  }
  return ans;
}
void solve() {
  int r, c;
  cin >> r >> c;
  bfsN = r, bfsM = c;
  ((arr).resize(r));
  cin >> arr;
  cout << BFS(0, 0) << "\n";
}
void initialise() { return; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t = 1;
  initialise();
  for (int i = 1; i < t + 1; ++i) {
    solve();
  }
  return 0;
}
