#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct point {
  int x, y, cost;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  int H, W;
  cin >> H >> W;
  vector<string> maze(H);
  for (int(i) = 0; (i) < (H); (i)++) {
    cin >> maze[i];
  }
  point start;
  bool ready = false;
  for (int(i) = 0; (i) < (H); (i)++) {
    for (int(j) = 0; (j) < (W); (j)++) {
      if (maze[i][j] == '.') {
        start.x = i;
        start.y = j;
        start.cost = 0;
        ready = true;
        break;
      }
    }
    if (ready) {
      break;
    }
  }
  queue<point> q;
  q.push(start);
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  bool used[H][W];
  memset(used, 0, sizeof(used));
  used[start.y][start.x] = true;
  while (!q.empty()) {
    point cp = q.front();
    q.pop();
    int x = cp.x;
    int y = cp.y;
    for (int(i) = 0; (i) < (4); (i)++) {
      if (0 <= x + dx[i] && x + dx[i] < W)
        if (0 <= y + dy[i] && y + dy[i] < H)
          if (maze[y + dy[i]][x + dx[i]] == '.')
            if (!used[y + dy[i]][x + dx[i]]) {
              point tmp;
              tmp.x = x + dx[i];
              tmp.y = y + dy[i];
              tmp.cost = cp.cost + 1;
              q.push(tmp);
              used[y + dy[i]][x + dx[i]] = true;
            }
    }
    if (q.empty()) {
      start = cp;
      start.cost = 0;
    }
  }
  q.push(start);
  bool used2[H][W];
  memset(used2, 0, sizeof(used2));
  used2[start.y][start.x] = true;
  while (!q.empty()) {
    point cp = q.front();
    q.pop();
    int x = cp.x;
    int y = cp.y;
    for (int(i) = 0; (i) < (4); (i)++) {
      if (0 <= x + dx[i] && x + dx[i] < W)
        if (0 <= y + dy[i] && y + dy[i] < H)
          if (maze[y + dy[i]][x + dx[i]] == '.')
            if (!used2[y + dy[i]][x + dx[i]]) {
              point tmp;
              tmp.x = x + dx[i];
              tmp.y = y + dy[i];
              tmp.cost = cp.cost + 1;
              q.push(tmp);
              used2[tmp.y][tmp.x] = true;
            }
    }
    if (q.empty()) {
      cout << cp.cost << endl;
      return 0;
    }
  }
  return 0;
}
