#include <bits/stdc++.h>
using namespace std;
bool masu[25][25];
int h, w;
int solve(int sx, int sy, int gx, int gy) {
  int dist[25][25];
  for (int i = 0; i < 25; i++) {
    for (int j = 0; j < 25; j++) {
      dist[i][j] = -1;
    }
  }
  queue<pair<int, int>> que;
  dist[sx][sy] = 0;
  pair<int, int> now;
  now.first = sx;
  now.second = sy;
  que.push(now);
  while (!que.empty()) {
    pair<int, int> sentou = que.front();
    que.pop();
    for (int i = sentou.first - 1; i <= sentou.first + 1; i++) {
      for (int j = sentou.second - 1; j <= sentou.second + 1; j++) {
        if (i < 1 || i > h || j < 1 || j > w) continue;
        if (dist[i][j] != -1) continue;
        if (masu[i][j] == false) continue;
        dist[i][j] = dist[sentou.first][sentou.second] + 1;
        if (i == gx && j == gy) {
          int bug = dist[i][j];
          return dist[i][j];
        }
        que.push(sentou);
      }
    }
  }
}
int main(void) {
  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char s;
      cin >> s;
      if (s == '.')
        masu[i][j] = true;
      else
        masu[i][j] = false;
    }
  }
  int stax;
  int stay;
  int gox;
  int goy;
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (masu[i][j] == false)
        continue;
      else {
        stax = i;
        stay = j;
      }
      for (int k = j; k <= h; k++) {
        for (int l = j; l <= w; l++) {
          if (stax == k && stay == l) continue;
          gox = k;
          goy = l;
        }
      }
    }
  }
  ans = solve(1, 1, 3, 3);
  cout << ans << endl;
}
