#include <bits/stdc++.h>
using namespace std;
int main() {
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  int h, w;
  cin >> h >> w;
  char c;
  int mz[h + 1][w + 1];
  int x, y;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> c;
      if (c == '.') {
        mz[i][j] = 0;
        x = i;
        y = j;
      } else {
        mz[i][j] = 1;
      }
    }
  }
  queue<pair<int, int>> q;
  int vis[h + 1][w + 1];
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      vis[i][j] = 0;
    }
  }
  q.push({x, y});
  vis[x][y] = 1;
  pair<int, int> cur;
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      x = cur.first + dx[i];
      y = cur.second + dy[i];
      if (x > h || x < 1 || y < 1 || y > w) {
        continue;
      }
      if (!vis[x][y] && mz[x][y] != 1) {
        vis[x][y] = 1;
        q.push({x, y});
      }
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      vis[i][j] = 0;
    }
  }
  while (!q.empty()) {
    q.pop();
  }
  x = cur.first;
  y = cur.second;
  q.push({x, y});
  vis[x][y] = 1;
  int depth[h + 1][w + 1];
  depth[x][y] = 0;
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      x = cur.first + dx[i];
      y = cur.second + dy[i];
      if (x > h || x < 1 || y < 1 || y > w) {
        continue;
      }
      if (!vis[x][y] && mz[x][y] != 1) {
        vis[x][y] = 1;
        q.push({x, y});
        depth[x][y] = 1 + depth[cur.first][cur.second];
      }
    }
  }
  cout << depth[cur.first][cur.second] << "\n";
  return 0;
}
