m写成了n。。。尼玛,wa死我了
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
#include<ctime>
using namespace std;
#ifdef _WIN32
typedef __int64 i64;
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
typedef long long i64;
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 211;
int n, m;
char s[maxn][maxn];
int go[4][2] = { { 0, -1 }, { -1, 0 }, { 1, 0 }, { 0, 1 } };
int key;
int sx, sy;
int ex, ey;
struct zz{
int x, y, hash;
bool operator < (const zz& cmp) const{
if (x != cmp.x) return x<cmp.x;
else if (y != cmp.y) return y<cmp.y;
else return hash<cmp.hash;
}
bool operator == (const zz&cmp){
return x == cmp.x && y == cmp.y && hash == cmp.hash;
}
};
map<zz, int>mp;
void gao(){
char c = '1';
key = 0;
int temp = 0;
for (int x = 1; x <= n; x++){
for (int y = 1; y <= m; y++){
if (s[x][y] == 'K'){
s[x][y] = c++;
key |= 1 << temp;
temp++;
}
if (s[x][y] == 'S'){
sx = x;
sy = y;
}
if (s[x][y] == 'E'){
ex = x;
ey = y;
}
}
}
}
bool yes(zz now){
return now.x <= n && now.x >= 1 && now.y <= m && now.y >= 1;
}
int get(char c)
{
if( c == 'L'){
return 0;
}
else if(c == 'U'){
return 1;
}
else if(c == 'D'){
return 2;
}
else if(c == 'R'){
return 3;
}else return -1;
}
set<zz>pass;
zz next(zz now,int d)
{
zz to = now;
int tx;
for(int step=1;;step++){
to.x += go[d][0];
to.y += go[d][1];
if(!yes(to)){
return to;
}
if(s[to.x][to.y] == '#'){
to.x -= go[d][0];
to.y -= go[d][1];
if(to == now){
to.x=-1;
return to;
}
else{
return to;
}
}
else if(s[to.x][to.y]>='1' && s[to.x][to.y]<='9'){
tx = s[to.x][to.y] - '1';
to.hash |= (1<<tx);
}
else if(s[to.x][to.y] == 'E'){
if(to.hash == key){
return to;
}
}
else if(get(s[to.x][to.y])!=-1){
int dx = get(s[to.x][to.y]);
if(pass.find(to) != pass.end()){
to.x = -1;
return to;
}
else{
pass.insert(to);;
return next(to,dx);
}
}
}
}
void spfa()
{
queue<zz>q;
zz now, to;
now.x = sx;
now.y = sy;
now.hash = 0;
mp.clear();
mp[now] = 0;
q.push(now);
int temp;
while (!q.empty()){
now = q.front();
q.pop();
for (int i = 0; i < 4; i++){
pass.clear();
to = next(now, i);
if (yes(to)){
temp = mp[now] + 1;
if (mp.find(to) == mp.end()){
mp[to] = temp;
q.push(to);
}
}
}
}
}
void start()
{
gao();
spfa();
return ;
}
int main()
{
while (cin >> n >> m){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> s[i][j];
}
}
start();
zz now;
now.x = ex;
now.y = ey;
now.hash = key;
if (mp.find(now) == mp.end()){
cout << -1 << endl;
}
else{
cout << mp[now] << endl;
}
}
return 0;
}