1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<queue>
5 using namespace std;
6 int bgx,bgy,edx,edy;
7 const int MAXN=1001;
8 int map[MAXN][MAXN];
9 bool vis[MAXN][MAXN];
10 int xx[6]={-1,+1,0,0};
11 int yy[6]={0,0,-1,+1};
12 int n,m;
13 struct node
14 {
15 int x;
16 int y;
17 int step;
18 }cur,nxt;
19 queue<node>q;
20 void bfs(int x,int y)
21 {
22 while(q.size()!=0)q.pop();
23 cur.x=x; cur.y=y; cur.step=0;
24 q.push(cur);
25 vis[x][y]=1;
26 int flag=0;
27 while(q.size()!=0)
28 {
29 node p=q.front();
30 q.pop();
31 for(int i=0;i<4;i++)
32 {
33 int wx=p.x+xx[i];
34 int wy=p.y+yy[i];
35 if(wx>=1&&wx<=n&&wy>=1&&wy<=m&&vis[wx][wy]==0&&map[wx][wy]!=1)
36 {
37 if(map[wx][wy]==2)
38 {
39 printf("%d
",p.step+1);
40 flag=1;
41 break;
42 }
43 else
44 {
45 nxt.x=wx;
46 nxt.y=wy;
47 nxt.step=p.step+1;
48 q.push(nxt);
49 vis[wx][wy]=1;
50 }
51 }
52 }
53 if(flag==1)break;
54 }
55 if(flag==0)
56 printf("-1
");
57 }
58 int main()
59 {
60 while(scanf("%d%d",&n,&m))
61 {
62 memset(map,0,sizeof(map));
63 memset(vis,0,sizeof(vis));
64 if(n==0&&m==0)break;
65 for(int i=1;i<=n;i++)
66 {
67 for(int j=1;j<=m;j++)
68 {
69 char c;
70 cin>>c;
71 if(c=='@') {bgx=i;bgy=j;map[i][j]=1;}
72 if(c=='*') {edx=i;edy=j;map[i][j]=2;}
73 if(c=='#') {map[i][j]=1;}
74 if(c=='.') {map[i][j]=0;}
75 }
76 }
77 bfs(bgx,bgy);
78 }
79 return 0;
80 }