#include <bits/stdc++.h>
const int INF = 1001001001;
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;
int map[102][102];
bool used[5000][102][102];
int main() {
  while (std::cin >> N >> M, N) {
    memset(used, 0, sizeof(used));
    memset(map, 0, sizeof(map));
    for (int i = (1); i < (M + 1); i++) {
      for (int j = (1); j < (N + 1); j++) {
        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<std::pair<int, std::pair<int, int>>,
                        std::vector<std::pair<int, std::pair<int, int>>>,
                        std::greater<std::pair<int, std::pair<int, int>>>>
        q;
    for (int i = (0); i < (N + 2); i++) {
      q.push(std::make_pair(0, std::make_pair(0, i)));
      q.push(std::make_pair(0, std::make_pair(M + 1, i)));
    }
    for (int i = (0); i < (M + 2); i++) {
      q.push(std::make_pair(0, std::make_pair(i, 0)));
      q.push(std::make_pair(0, std::make_pair(i, N + 1)));
    }
    int res = -1;
    while (!q.empty()) {
      std::pair<int, std::pair<int, int>> s = q.top();
      q.pop();
      int t = s.first, y = s.second.first, x = s.second.second;
      if (t >= 5000) {
        continue;
      }
      if (map[y][x] == 1) {
        res = t;
        break;
      }
      if (used[t][y][x]) {
        continue;
      }
      used[t][y][x] = true;
      for (int i = (0); i < (4); i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (0 <= nx && nx <= N + 1 && 0 <= ny && ny <= M + 1 &&
            !used[t][ny][nx] && map[ny][nx] != -1) {
          q.push(std::make_pair(t, std::make_pair(ny, nx)));
        }
      }
      for (int i = (0); i < (4); i++) {
        int mx = x + dx[i], my = y + dy[i], nx = x + dx[i] * 2,
            ny = y + dy[i] * 2;
        if (0 <= nx && nx <= N + 1 && 0 <= ny && ny <= M + 1 &&
            !used[t + 1][ny][nx] && map[ny][nx] != -1 && map[my][mx] == -1) {
          q.push(std::make_pair(t + 1, std::make_pair(ny, nx)));
        }
      }
    }
    assert(res != -1);
    std::cout << res << std::endl;
  }
}
