/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
int x, y;
this.x = x;
this.y = y;
}
}
public static void main
(String[] args
) { Scanner in
= new Scanner
(System.
in);
int n = in.nextInt(), m = in.nextInt();
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
in.nextLine();
int[][] dis = new int[n][m];
boolean[][] vis = new boolean[n][m];
Point start
= null, end
= null;
for (int i = 0; i < n; i++) {
ar[i] = in.nextLine();
for (int j = 0; j < m; j++) {
char ch = ar[i].charAt(j);
if (ch == 'E') {
} else if (ch == 'S') {
}
}
}
Queue<Point> q = new ArrayDeque<>();
if (end != null) {
q.add(end);
vis[end.x][end.y] = true;
}
int steps = 0;
while (!q.isEmpty()) {
int curDist = dis[p.x][p.y];
if (p.x == start.x && p.y == start.y) {
steps = curDist;
}
for (int i = 0; i < 4; i++) {
int newx = p.x + dx[i], newy = p.y + dy[i];
if (newx >= 0 && newx < n && newy >= 0 && newy < m
&& !vis[newx][newy] && ar[newx].charAt(newy) != 'T') {
q.
add(new Point(newx, newy
)); vis[newx][newy] = true;
dis[newx][newy] = curDist + 1;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char ch = ar[i].charAt(j);
if (Character.
isDigit(ch
) && vis
[i
][j
] && dis
[i
][j
] <= steps
) { ans += ch - '0';
}
}
}
in.close();
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXN0YXRpYyBjbGFzcyBQb2ludCB7CiAgICAgICAgaW50IHgsIHk7CiAgICAgICAgUG9pbnQoaW50IHgsIGludCB5KSB7CiAgICAgICAgICAgIHRoaXMueCA9IHg7CiAgICAgICAgICAgIHRoaXMueSA9IHk7CiAgICAgICAgfQogICAgfQogCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU2Nhbm5lciBpbiA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAKICAgICAgICBpbnQgbiA9IGluLm5leHRJbnQoKSwgbSA9IGluLm5leHRJbnQoKTsKICAgICAgICBpbnQgZHhbXSA9IHstMSwgMCwgMSwgMH07CiAgICAgICAgaW50IGR5W10gPSB7MCwgLTEsIDAsIDF9OwogICAgICAgIGluLm5leHRMaW5lKCk7CiAKICAgICAgICBTdHJpbmdbXSBhciA9IG5ldyBTdHJpbmdbbl07CiAgICAgICAgaW50W11bXSBkaXMgPSBuZXcgaW50W25dW21dOwogICAgICAgIGJvb2xlYW5bXVtdIHZpcyA9IG5ldyBib29sZWFuW25dW21dOwogICAgICAgIFBvaW50IHN0YXJ0ID0gbnVsbCwgZW5kID0gbnVsbDsKIAogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGFyW2ldID0gaW4ubmV4dExpbmUoKTsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgIGNoYXIgY2ggPSBhcltpXS5jaGFyQXQoaik7CiAgICAgICAgICAgICAgICBpZiAoY2ggPT0gJ0UnKSB7CiAgICAgICAgICAgICAgICAgICAgZW5kID0gbmV3IFBvaW50KGksIGopOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChjaCA9PSAnUycpIHsKICAgICAgICAgICAgICAgICAgICBzdGFydCA9IG5ldyBQb2ludChpLCBqKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KIAogICAgICAgIFF1ZXVlPFBvaW50PiBxID0gbmV3IEFycmF5RGVxdWU8PigpOwogICAgICAgIGlmIChlbmQgIT0gbnVsbCkgewogICAgICAgICAgICBxLmFkZChlbmQpOwogICAgICAgICAgICB2aXNbZW5kLnhdW2VuZC55XSA9IHRydWU7CiAgICAgICAgfQogCiAgICAgICAgaW50IHN0ZXBzID0gMDsKIAogICAgICAgIHdoaWxlICghcS5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgUG9pbnQgcCA9IHEucG9sbCgpOwogICAgICAgICAgICBpbnQgY3VyRGlzdCA9IGRpc1twLnhdW3AueV07CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAocC54ID09IHN0YXJ0LnggJiYgcC55ID09IHN0YXJ0LnkpIHsKICAgICAgICAgICAgICAgIHN0ZXBzID0gY3VyRGlzdDsKICAgICAgICAgICAgfQogCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgNDsgaSsrKSB7CiAgICAgICAgICAgICAgICBpbnQgbmV3eCA9IHAueCArIGR4W2ldLCBuZXd5ID0gcC55ICsgZHlbaV07CiAgICAgICAgICAgICAgICBpZiAobmV3eCA+PSAwICYmIG5ld3ggPCBuICYmIG5ld3kgPj0gMCAmJiBuZXd5IDwgbSAKICAgICAgICAgICAgICAgICAgICAgICAgJiYgIXZpc1tuZXd4XVtuZXd5XSAmJiBhcltuZXd4XS5jaGFyQXQobmV3eSkgIT0gJ1QnKSB7CiAgICAgICAgICAgICAgICAgICAgcS5hZGQobmV3IFBvaW50KG5ld3gsIG5ld3kpKTsKICAgICAgICAgICAgICAgICAgICB2aXNbbmV3eF1bbmV3eV0gPSB0cnVlOwogICAgICAgICAgICAgICAgICAgIGRpc1tuZXd4XVtuZXd5XSA9IGN1ckRpc3QgKyAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogCiAgICAgICAgaW50IGFucyA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgIGNoYXIgY2ggPSBhcltpXS5jaGFyQXQoaik7CiAgICAgICAgICAgICAgICBpZiAoQ2hhcmFjdGVyLmlzRGlnaXQoY2gpICYmIHZpc1tpXVtqXSAmJiBkaXNbaV1bal0gPD0gc3RlcHMpIHsKICAgICAgICAgICAgICAgICAgICBhbnMgKz0gY2ggLSAnMCc7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oYW5zKTsKICAgICAgICBpbi5jbG9zZSgpOwogICAgfQp9