Q.pop();
for(int i = 0; i < 4; ++i)
{
next.x = tp.x+dx[i];
next.y = tp.y+dy[i];
if(inmap(next) && vis[next.x][next.y]==-1&&sp[next.x][next.y]!='T')
{
vis[next.x][next.y] = vis[tp.x][tp.y]+1;
Q.push(next);
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", sp[i]);
int flag = 0;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
if(sp[i][j] == 'S')
{
st.x = i;
st.y = j;
if(flag == 1)
{
i = n;
j = m;
}
else
flag = 1;
}
if(sp[i][j] == 'E')
{
ed.x = i;
ed.y = j;
if(flag == 1)
{
i = n;
j = m;
}
else
flag = 1;
}
}
}
BFS();
int cnt = 0;
int MAX = vis[st.x][st.y];
for(int i = 0; i
for(int j = 0; j < m; ++j)
{
if(sp[i][j]>='1'&& sp[i][j]<='9')
if(vis[i][j] <= MAX && vis[i][j] != -1)
cnt+=(sp[i][j] - '0');
}
}
cout << cnt <
}