#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int result, endx, endy;
vector<int> xd = {0, 1, 0, -1};
vector<int> yd = {1, 0, -1, 0};
void bfs(int l, int c, int n, int m, vector<vector<char>> &grid) {
  if (grid[l][c] == '#') return;
  vector<vector<int>> dist(n, vector<int>(m, -1));
  dist[l][c] = 0;
  vector<pair<int, int>> q;
  q.push_back({l, c});
  for (int i = 0; i < q.size(); ++i) {
    pair<int, int> x = q[i];
    for (int i = 0; i < 4; ++i) {
      pair<int, int> y = {x.first + xd[i], x.second + yd[i]};
      if (y.first < 0 || y.first > n - 1 || y.second < 0 || y.second > m - 1 ||
          grid[y.first][y.second] == '#')
        continue;
      if (dist[y.first][y.second] == -1 && grid[y.first][y.second] == '.') {
        dist[y.first][y.second] = dist[x.first][x.second] + 1;
        endx = y.first;
        endy = y.second;
        result = dist[y.first][y.second];
        q.push_back(y);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int xstart, ystart;
  int n, m;
  cin >> n >> m;
  vector<vector<char>> grid(n + 1, vector<char>(m + 1));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> grid[i][j];
      if (grid[i][j] == '.') {
        xstart = i;
        ystart = j;
      }
    }
  bfs(0, 0, n, m, grid);
  bfs(endx, endy, n, m, grid);
  cout << result << '\n';
}
