#include <bits/stdc++.h>
using namespace std;
int map[20][20];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool used[20][20];
int k[20][20];
vector<int> v;
set<pair<int, int> > s;
int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    string tmp;
    cin >> tmp;
    for (int j = 0; j < w; j++) {
      switch (tmp[j]) {
        case '.':
          map[i][j] = 0;
          break;
        case '#':
          map[i][j] = -1;
          break;
      }
    }
  }
  memset(used, false, sizeof(used));
  memset(k, 0x7f, sizeof(k));
  queue<pair<int, int> > q;
  q.push(make_pair(0, 0));
  k[0][0] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    used[p.first][p.second] = true;
    bool flag = false;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || map[nx][ny] == -1 ||
          used[nx][ny])
        continue;
      flag = true;
      k[nx][ny] = min(k[nx][ny], k[p.first][p.second] + 1);
      q.push(make_pair(nx, ny));
    }
    pair<int, int> PP = make_pair(p.first, p.second);
    if (!flag && s.find(PP) == s.end()) {
      s.insert(make_pair(p.first, p.second));
      v.push_back(k[p.first][p.second]);
    }
  }
  if (v.size() == 1) {
    cout << v[0] << endl;
    return 0;
  }
  sort(v.begin(), v.end());
  cout << v[v.size() - 1] + v[v.size() - 2] << endl;
  return 0;
}
