#include <bits/stdc++.h>
namespace solution {
class SolutionInterface {
 public:
  virtual int run() = 0;

 protected:
  virtual void pre_calc() {}
  virtual bool action() = 0;
  virtual void init(){};
  virtual bool input() { return false; };
  virtual void output(){};
  SolutionInterface() {}

 private:
};
}  // namespace solution
namespace solution {
class SolutionBase : public SolutionInterface {
 public:
  virtual int run() {
    pre_calc();
    while (action())
      ;
    return 0;
  }
};
}  // namespace solution
namespace solution {}
namespace solution {
using namespace std;
}
namespace solution {
const int SIZE = 1000 + 11;
const int INF = std::numeric_limits<int>::max();
const char CHAR_START = 'S';
const char CHAR_EXIT = 'E';
const char CHAR_ZERO = '0';
const char CHAR_TREE = 'T';
const int dr[4] = {1, -1, 0, 0};
const int dc[4] = {0, 0, 1, -1};
int H;
int W;
string S[SIZE];
int MC[SIZE][SIZE];
int min_dist[SIZE][SIZE];
int exit_r;
int exit_c;
int start_r;
int start_c;
int result;
}  // namespace solution
namespace solution {
class Solver {
 public:
  void solve() {
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (S[i][j] == CHAR_START) {
          start_r = i;
          start_c = j;
          S[i][j] = CHAR_ZERO;
        } else if (S[i][j] == CHAR_EXIT) {
          exit_r = i;
          exit_c = j;
          S[i][j] = CHAR_ZERO;
        }
      }
    }
    fill_min_dist();
    int min_steps = get_minimum_steps();
    result = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (S[i][j] != CHAR_TREE && min_dist[i][j] <= min_steps) {
          result += S[i][j] - '0';
        }
      }
    }
  }
  int get_minimum_steps() {
    std::priority_queue<std::pair<int, std::pair<int, int> >,
                        std::vector<std::pair<int, std::pair<int, int> > >,
                        std::greater<std::pair<int, std::pair<int, int> > > >
        Q;
    Q.push(std::pair<int, std::pair<int, int> >(
        0, std::pair<int, int>(start_r, start_c)));
    MC[start_r][start_c] = 0;
    while (!Q.empty()) {
      std::pair<int, std::pair<int, int> > node = Q.top();
      Q.pop();
      int steps = node.first;
      int next_steps = steps + 1;
      int r = node.second.first;
      int c = node.second.second;
      if (r == exit_r && c == exit_c) return steps;
      for (int k = 0; k < 4; ++k) {
        int nr = r + dr[k];
        int nc = c + dc[k];
        if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
        if (S[nr][nc] == CHAR_TREE) continue;
        if (next_steps >= MC[nr][nc]) continue;
        MC[nr][nc] = next_steps;
        Q.push(std::pair<int, std::pair<int, int> >(
            next_steps, std::pair<int, int>(nr, nc)));
      }
    }
    return -1;
  }
  void fill_min_dist() {
    std::queue<std::pair<int, int> > Q;
    Q.push(std::pair<int, int>(exit_r, exit_c));
    min_dist[exit_r][exit_c] = 0;
    while (!Q.empty()) {
      std::pair<int, int> node = Q.front();
      Q.pop();
      int r = node.first;
      int c = node.second;
      int next_steps = min_dist[r][c] + 1;
      for (int k = 0; k < 4; ++k) {
        int nr = r + dr[k];
        int nc = c + dc[k];
        if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
        if (S[nr][nc] == CHAR_TREE) continue;
        if (next_steps >= min_dist[nr][nc]) continue;
        min_dist[nr][nc] = next_steps;
        Q.push(std::pair<int, int>(nr, nc));
      }
    }
  }

 private:
};
}  // namespace solution
namespace solution {
class Solution : public SolutionBase {
 public:
 protected:
  virtual bool action() {
    init();
    if (!input()) return false;
    solver.solve();
    output();
    return true;
  }
  void init() {
    for (int i = 0; i < SIZE; ++i)
      for (int j = 0; j < SIZE; ++j) min_dist[i][j] = INF;
    for (int i = 0; i < SIZE; ++i)
      for (int j = 0; j < SIZE; ++j) MC[i][j] = INF;
  }
  bool input() {
    if (!(cin >> H >> W)) return false;
    for (int i = 0; i < H; ++i) cin >> S[i];
    return true;
  }
  void output() { cout << result << endl; }

 private:
  Solver solver;
};
}  // namespace solution
int main() { return solution::Solution().run(); }
