#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int d[20][20];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
vector<string> maze(50);
int r, c, sy, sx, gy, gx;
int bfs() {
  queue<pair<int, int> > que;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      d[i][j] = INF;
    }
  }
  que.push(pair<int, int>(sy - 1, sx - 1));
  d[sy - 1][sx - 1] = 0;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first == gy - 1 && p.second == gx - 1) break;
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      if (0 <= nx && nx < c && 0 <= ny && ny < r && maze[ny][nx] != '#' &&
          d[ny][nx] == INF) {
        que.push(pair<int, int>(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gy - 1][gx - 1];
}
int main() {
  int res = 0;
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> maze[i];
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < c; j++) {
      if (maze[i][j] == '.') gx = j;
    }
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (maze[i][j] == '.') {
        sy = i;
        sx = j;
        for (int k = 0; k < r; k++) {
          if (maze[k][gx] == '.') {
            gy = k;
            int res = min(res, bfs());
          }
        }
      }
    }
  }
  cout << res << endl;
}
