#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long h, w;
  cin >> h >> w;
  char mp[h][w];
  pair<long long, long long> st;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == '.') {
        st.first = i;
        st.second = j;
      }
    }
  }
  queue<pair<long long, long long> > bfs;
  pair<long long, long long> nex;
  bfs.push(st);
  mp[st.first][st.second] = '!';
  while (!bfs.empty()) {
    nex = bfs.front();
    bfs.pop();
    for (long long i = 0; i < 4; i++) {
      if (nex.first + dx[i] >= 0 && nex.first + dx[i] < h &&
          nex.second + dy[i] >= 0 && nex.second + dy[i] < w) {
        if (mp[nex.first + dx[i]][nex.second + dy[i]] == '.') {
          mp[nex.first + dx[i]][nex.second + dy[i]] = '!';
          bfs.push(make_pair(nex.first + dx[i], nex.second + dy[i]));
        }
      }
    }
  }
  bfs.push(nex);
  long long mp2[h][w];
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      mp2[i][j] = -1;
    }
  }
  mp2[nex.first][nex.second] = 0;
  while (!bfs.empty()) {
    nex = bfs.front();
    bfs.pop();
    for (long long i = 0; i < 4; i++) {
      if (nex.first + dx[i] >= 0 && nex.first + dx[i] < h &&
          nex.second + dy[i] >= 0 && nex.second + dy[i] < w) {
        if (mp[nex.first + dx[i]][nex.second + dy[i]] == '!') {
          mp[nex.first + dx[i]][nex.second + dy[i]] = '?';
          mp2[nex.first + dx[i]][nex.second + dy[i]] =
              mp2[nex.first][nex.second] + 1;
          bfs.push(make_pair(nex.first + dx[i], nex.second + dy[i]));
        }
      }
    }
  }
  cout << mp2[nex.first][nex.second] << "\n";
  return 0;
}
