1 #include<queue>
2 #include<cstdio>
3 #include<cstring>
4 #include<iostream>
5 #include<algorithm>
6 using namespace std;
7 struct abd
8 {
9 int x;
10 int y,step;
11 } cur,nxt;
12 queue<abd>que;
13 int mp[25][25];
14 bool v[25][25];
15 int moe[5] = {1,0,-1,0,1};
16 int n,m,li[2];
17 int tox,toy;
18 void bfs(int xx,int xy)
19 {
20 while(!que.empty())que.pop();
21 memset(v,0,sizeof(v));
22 cur.x=xx;
23 cur.y=xy;
24 cur.step=0;
25 que.push(cur);
26 v[xx][xy]=1;
27 while(!que.empty())
28 {
29 cur=que.front();
30 que.pop();
31 for(int i=0; i<4; i++)
32 {
33 int xxx=cur.x+moe[i];
34 int yyy=cur.y+moe[i+1];
35 if(xxx==tox&&yyy==toy)
36 {
37 cout<<cur.step+1<<endl;
38 return;
39 }
40 else if(xxx<=n&&xxx>=1&&yyy>=1&&yyy<=m&&!mp[xxx][yyy]&&!v[xxx][yyy])
41 {
42 nxt.x=xxx;
43 nxt.y=yyy;
44 nxt.step=cur.step+1;
45 v[xxx][yyy]=1;
46 que.push(nxt);
47 }
48 }
49 }
50 cout<<-1<<endl;
51 }
52 int main() {
53 string a;
54 while(cin>>n>>m)
55 {
56 if(n==0&&m==0)break;
57 for(int i=1; i<=n; i++)
58 {
59 cin>>a;
60 for(int j=0; j<a.size(); j++)
61 {
62 switch(a[j])
63 {
64 case '.' :mp[i][j+1]=0;
65 break;
66 case '#' :mp[i][j+1]=2; break;
67 case '*' :mp[i][j+1]=0;tox=i;toy=j+1;break;
68 case '@' :mp[i][j+1]=3;li[0]=i;li[1]=j+1;break;
69 }
70 }
71 }
72 bfs(li[0],li[1]);
73 }
74 return 0;
75 }