#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <cstring>
#include <cassert>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N, M; // N: x, M: y
int map[102][102];
bool used[1000][102][102];

int main(){
    while(std::cin >> N >> M, N){
        memset(used, 0, sizeof(used));
        memset(map, 0, sizeof(map));

        FOR(i, 1, M+1){
            FOR(j, 1, N+1){
                char c;
                std::cin >> c;
                if(c == '.'){map[i][j] = 0;}
                else if(c == '#'){map[i][j] = -1;}
                else{map[i][j] = 1;}
            }
        }
    
        std::priority_queue<State,std::vector<State>,std::greater<State>> q;
        REP(i, N+2){
            q.push(mp(0, mp(0, i)));
            q.push(mp(0, mp(M+1, i)));
        }

        REP(i, M+2){
            q.push(mp(0, mp(i, 0)));
            q.push(mp(0, mp(i, N+1)));
        }

        int res = -1;
        while(!q.empty()){
            State s = q.top(); q.pop();
            int t = s.first, y = s.second.first, x = s.second.second;

            if(t >= 1000){continue;}
            if(map[y][x] == 1){res = t; break;}
            if(used[t][y][x]){continue;}

            used[t][y][x] = true;
        
            REP(i, 4){
                int nx = x + dx[i], ny = y + dy[i];
                if(0 <= nx && nx <= N+1 &&
                   0 <= ny && ny <= M+1){
                    if(map[y][x] == 0 && map[ny][nx] == -1){
                        if(!used[t+1][ny][nx]){
                            q.push(mp(t+1, mp(ny, nx)));
                        }
                    }else{
                        q.push(mp(t, mp(ny, nx)));
                    }
                }
            }
        }

        assert(res != -1);
        std::cout << res << std::endl;
    }
}