#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef pair<int , int> PII;
const int N = 2010;
int n , m;
int d[N][N];
PII q[N * N];
char a[N][N];
void bfs (int x , int y) {
memset (d , -1 , sizeof (d));
d[x][y] = 0;
int hh = 0 , tt = 0;
q[0] = {x , y};
int dx[4] = {-1 , 0 , 1 , 0};
int dy[4] = {0 , 1 , 0 , -1};
while (hh <= tt) {
auto t = q[hh ++];
for (int i = 0; i < 4; ++ i) {
int tx = t.first + dx[i];
int ty = t.second + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && d[tx][ty] == -1 && a[tx][ty] != '#') {
d[tx][ty] = d[t.first][t.second] + 1;
q[tt ++] = {tx , ty};
}
}
}
}
int main() {
cin >> n >> m;
int x , y;
int sx = 0 , sy = 0;
for (int i = 1; i <= n; ++ i)
for (int j = 1; j <= m; ++ j) {
cin >> a[i][j];
if (a[i][j] == 'd') x = i , y = j;
if (a[i][j] == 'm') sx = i , sy = j;
}
bfs (x , y);
if (d[sx][sy] == -1) puts("No Way!");
else cout << d[sx][sy] << endl;
return 0;
}