#include<bits/stdc++.h>
using namespace std;
int n,m,ret,sum;
char mp[2005][2005];
bool vis[2005][2005];
int sx,sy,fx,fy;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
struct zhao
{
int x;
int y;
int step;
}mm,nn;
int bfs(int x,int y)
{
queue<zhao> q;
mm.x = x; mm.y = y;mm.step = 0;
q.push(mm);
vis[x][y] = 1;
while(!q.empty())
{
mm = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
int nx = dx[i]+mm.x;
int ny = dy[i]+mm.y;
if(nx<1 || nx>n || ny<1 || ny>m || mp[nx][ny]=='#' || vis[nx][ny]==1)
continue;
nn.x = nx; nn.y = ny; nn.step = mm.step+1;
vis[nx][ny] = 1;
q.push(nn);
if(mp[nx][ny]=='d')
return nn.step;
}
}
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>mp[i][j];
if(mp[i][j]=='m')
sx = i,sy = j;
else if(mp[i][j]=='d')
fx = i,fy = j;
}
}
ret = bfs(sx,sy);
if(ret>0)
cout<<ret;
else
cout<<"No Way!";
return 0;
}