#include <bits/stdc++.h>
using namespace std;
char ara[30][30];
int visit[30][30], h, w;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue<pair<int, int> > q;
int bfs() {
  int x, y, a, b, i;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    for (i = 0; i < 4; i++) {
      a = dx[i] + x, b = dy[i] + y;
      if (a < 0 || b < 0 || a == h || b == w || visit[a][b] != -1 ||
          ara[a][b] == '#')
        continue;
      q.push({a, b});
      visit[a][b] = visit[x][y] + 1;
      return visit[a][b];
    }
  }
  return -1;
}
int main() {
  int i, j;
  scanf("%d %d", &h, &w);
  for (i = 0; i < h; i++) scanf("%s", ara[i]);
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++) visit[i][j] = -1;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (visit[i][j] == -1 && ara[i][j] == '.') {
        visit[i][j] = 0;
        q.push({i, j});
        int res = bfs();
        if (res != -1) {
          printf("%d\n", res);
          return 0;
        }
      }
    }
  }
  return 0;
}
