#include using namespace std; typedef long long ll; typedef pair P; int main(){ int dx[5]={1,-1,0,0},dy[5]={0,0,1,-1}; int w,h,a[30][30],b[30][30],i,j; scanf("%d %d\n",&h,&w); for(i=0;i<=w+1;i++){ for(j=0;j<=h+1;j++){ a[i][j]=2,b[i][j]=(1<<29); } } queue

que; for(i=1;i<=w;i++){ for(j=1;j<=h;j++){ char s; scanf("%c",&s); if(s=='.'){ a[i][j]=0; if(que.empty()){ que.push(P(i,j)); b[i][j]=0; } } else{ a[i][j]=1; } } scanf("\n"); } while(!que.empty()){ P p=que.front(); que.pop(); for(i=0;i<4;i++){ if(a[p.first+dx[i]][p.second+dy[i]]==0 && b[p.first+dx[i]][p.second+dy[i]]>b[p.first][p.second]){ b[p.first+dx[i]][p.second+dy[i]]=b[p.first][p.second]; que.push(P(p.first+dx[i],p.second+dy[i])); } if(a[p.first+dx[i]][p.second+dy[i]]==1 && b[p.first+dx[i]][p.second+dy[i]]>b[p.first][p.second]+1){ b[p.first+dx[i]][p.second+dy[i]]=b[p.first][p.second]+1; que.push(P(p.first+dx[i],p.second+dy[i])); } } } for(i=1;i<=w;i++){ for(j=1;j<=h;j++){ if(a[i][j]==0 && b[i][j]!=0){ cout << b[i][j] << endl; return 0; } } } }