#include <iostream>
#define H 12
using namespace std;
int movem[8][2] = {{-2,1}, {-1,2}, {1,2}, {2,1},
{2,-1}, {1,-2}, {-1,-2}, {-2,-1}};
int move_m[8][2] = {{-1,0}, {0,1}, {0,1}, {1,0}, {1,0}, {0,-1}, {0,-1}, {-1,0}};
int move[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool h[150][150][150];
char map[11][11];
int nn, mm;
int ci, cj, mi, mj, pi, pj, si, sj;
struct node
{
int c, m, p, step;
}que[1000001];
int open, close;
int c, m, p, ans;
node t, next;
int hash(int a,int b)
{return a * H + b;}
void read_map()
{
int i, j;
getchar();
for(i = 0; i < nn; i ++) gets(map[i]);
ci = cj = mi = mj = pi = pj = -1;
for(i = 0; i < nn; i ++)
for(j = 0; j < mm; j ++)
{
if(map[i][j] == 'C')
{ci = i; cj = j; map[i][j] = '.';}
if(map[i][j] == 'P')
{pi = i; pj = j; map[i][j] = '.';}
if(map[i][j] == 'M')
{mi = i; mj = j; map[i][j] = '.';}
if(map[i][j] == 'S')
{si = i; sj = j;}
}
}
bool check_c(node st, int x, int y)
{
if(x<0 || y<0 || x>=nn || y>=mm) return false;
if(h[st.c][st.m][st.p]) return false;
if(map[x][y] == 'D' || hash(x, y) == st.m || hash(x, y) == st.p) return false;
return true;
}
bool isok(int x, int y)
{
if(x==si && y==sj) return true;
return false;
}
void ex_c()
{
next = t; next.step ++;
int x, y, tx, ty;
x = next.c / H; y = next.c % H;
for(int i = 0; i < 4; i ++)
{
tx = x + move[i][0];
ty = y + move[i][1];
next.c = hash(tx, ty);
while(check_c(next, tx, ty))
{
//printf("ju: %d %d -> %d %d %d\n",x, y, tx, ty, ans);
if(isok(tx, ty))
{
ans = next.step;
return;
}
h[next.c][next.m][next.p] = 1;
que[++open] = next;
tx += move[i][0];
ty += move[i][1];
next.c = hash(tx, ty);
}
}
}
bool check_for_m(int x, int y, node st, int key)
{
int xx = x+move_m[key][0], yy = y+move_m[key][1];
//撇马脚
if(xx < 0 || yy < 0 || xx >= nn || yy >= mm) return false;
if(map[xx][yy] == 'D' || hash(xx, yy) == st.c || hash(xx, yy) == st.p || hash(xx, yy) == hash(si, sj))
return false;
xx = st.m / H;
yy = st.m % H;
if(xx < 0 || xx >= nn || yy < 0 || yy >= mm ) return false;
if(st.m == st.c || st.m == st.p || map[xx][yy] == 'D' || h[st.c][st.m][st.p]) return false;
return true;
}
void ex_m()
{
next = t; next.step ++;
int x, y, tx, ty;
x = next.m / H; y = next.m % H;
for(int i = 0; i < 8; i ++)
{
tx = x + movem[i][0]; ty = y + movem[i][1];
next.m = hash(tx, ty);
if(check_for_m(x, y, next, i))
{
//printf("ma: %d %d -> %d %d %d\n",x, y, tx, ty, ans);
if(isok(tx, ty))
{
ans = next.step;
return;
}
h[next.c][next.m][next.p] = 1;
que[++open] = next;
}
}
}
bool isok_p(node st)
{
int x, y, inc, cnt;
x = st.p / H; y = st.p % H;
if(x == si)
{
cnt = 0; inc = 1;
if(y > sj) inc = -1;
for(int j = y;; j += inc)
{
if(map[x][j] == 'D' || hash(x, j) == st.c || hash(x, j) == st.m)
cnt ++;
if(j == sj) break;
}
if(cnt == 1) return true;
return false;
}
if(y == sj)
{
cnt = 0; inc = 1;
if(x > si) inc = -1;
for(int i = x;; i += inc)
{
if(map[i][y] == 'D' || hash(i, y) == st.c || hash(i, y) == st.m)
cnt ++;
if(i == si) break;
}
if(cnt == 1) return true;
return false;
}
return false;
}
bool check_p(node st, int x, int y)
{
if(x<0 || y<0 || x>=nn || y>=mm) return false;
if(h[st.c][st.m][st.p]) return false;
if(map[x][y] == 'D' || hash(x, y)==st.m || hash(x, y)==st.c) return false;
if(hash(x, y) == hash(si, sj)) return false;
return true;
}
void ex_p()
{
next = t; next.step ++;
int x, y, tx, ty;
x = next.p / H; y = next.p % H;
if(isok_p(next))
{
ans = next.step;
return;
}
for(int i = 0; i < 4; i ++)
{
tx = x + move[i][0];
ty = y + move[i][1];
next.p = hash(tx, ty);
while(check_p(next, tx, ty))
{
//printf("ju: %d %d -> %d %d %d\n",x, y, tx, ty, ans);
h[next.c][next.m][next.p] = 1;
que[++open] = next;
tx += move[i][0];
ty += move[i][1];
next.p = hash(tx, ty);
}
}
}
void bfs()
{
open = 0; close = -1;
c = hash(ci, cj); m = hash(mi, mj); p = hash(pi, pj);
memset(h,0,sizeof(h));
h[c][m][p] = 1;
que[0].c = c; que[0].m = m; que[0].p = p;
que[0].step = 0;
while(close < open)
{
t = que[++close];
ex_c(); if(ans > 0) return;
ex_m(); if(ans > 0) return;
ex_p(); if(ans > 0) return;
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d", &nn, &mm) != EOF)
{
ans = 0;
read_map();
bfs();
if(ans > 0) printf("%d\n",ans);
else puts("OH!That's impossible!");
puts("");
}
//while(1);
}