#include <bits/stdc++.h>
using namespace std;
const int INF_INT = 2147483647;
const long long INF_LL = 9223372036854775807LL;
const unsigned long long INF_ULL = 18446744073709551615Ull;
const long long P = 92540646808111039LL;
const int Move[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
const int Move_[8][2] = {-1, 0, -1, -1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1};
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  os << "( " << p.first << " , " << p.second << " )";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto ite = (v).begin(); ite != (v).end(); ++ite)
    os << (ite - v.begin()) << " : " << *ite << endl;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << " { ";
  for (auto ite = (v).begin(); ite != (v).end(); ++ite) {
    os << *ite;
    if (ite != --v.end()) {
      os << " , ";
    }
  }
  os << " } ";
  return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& m) {
  for (auto ite = (m).begin(); ite != (m).end(); ++ite) {
    os << ite->first << "\t\t|->\t\t" << ite->second << endl;
  }
  return os;
}
int w, h;
string s[25];
int searched[25][25];
int find_t(int a1, int a2, int b1, int b2) {
  memset(searched, 0xff, sizeof(searched));
  searched[a1][a2] = 0;
  if (searched[b1][b2] >= 0) return 0;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(a1, a2));
  while (!q.empty()) {
    pair<int, int> curp = q.front();
    q.pop();
    for (long long i = 0; i < (4); i++) {
      int na1 = curp.first + Move[i][0];
      int na2 = curp.second + Move[i][1];
      if (na1 < 1 || na2 < 1 || na1 > h || na2 > w || s[na1][na2] == '#' ||
          searched[na1][na2] >= 0)
        continue;
      else
        searched[na1][na2] = searched[curp.first][curp.second] + 1;
      q.push(pair<int, int>(na1, na2));
      if (0) {
      };
      if (0) {
      };
    }
  }
  return searched[b1][b2];
}
int main() {
  cin >> h >> w;
  for (long long i = 1; i <= (h); i++) {
    s[i].push_back('_');
    string cur;
    cin >> cur;
    s[i] += cur;
  }
  for (long long i = 1; i <= (h); i++)
    if (0) {
    };
  pair<int, int> pos[4];
  pos[0] = pair<int, int>(1, 1);
  pos[1] = pair<int, int>(h, 1);
  pos[2] = pair<int, int>(1, w);
  pos[3] = pair<int, int>(h, w);
  int res = 0;
  for (long long i = 0; i < (4); i++)
    for (long long j = 0; j < (4); j++)
      if (pos[i] == pos[j])
        continue;
      else {
        int curcal =
            find_t(pos[i].first, pos[i].second, pos[j].first, pos[j].second);
        if (curcal < 0) continue;
        (res) = max(res, curcal);
        if (0) {
        };
        if (0) {
        };
        if (0) {
        };
      }
  cout << (res) << endl;
  return 0;
}
