#include <bits/stdc++.h>
using namespace std;
int r, c, sy = 1, sx = 1, gy, gx;
char ch[55][55];
int ans[55][55] = {0}, flag[55][55];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
queue<pair<int, int> > que;
int maxx = 0;
int maxy = 0;
int main() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> ch[i][j];
    }
  }
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < r; j++) {
      if (ch[i][j] == '.') {
        sx = i, sy = j;
        goto END;
      }
    }
  }
END:
  flag[sy][sx] = 1;
  que.push(pair<int, int>(sy, sx));
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int x = p.second + dx[i];
      int y = p.first + dy[i];
      if (0 <= x && x <= c && 0 <= y && y <= r && !flag[y][x] &&
          ch[y][x] == '.') {
        flag[y][x] = 1;
        ans[y][x] = ans[p.first][p.second] + 1;
        que.push(pair<int, int>(y, x));
      }
    }
  }
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (ans[maxx][maxy] < ans[i][j]) {
        maxx = i;
        maxy = j;
      }
    }
  }
  int cnt = ans[maxx][maxy];
  while (1) {
    int tmp = ans[maxx][maxy] - 1;
    ans[maxx][maxy] = -ans[maxx][maxy];
    for (int i = 0; i < 4; i++) {
      if (maxx + dx[i] < 0) {
        continue;
      }
      if (maxy + dy[i] < 0) {
        continue;
      }
      if (tmp == ans[maxx + dx[i]][maxy + dy[i]]) {
        maxx = maxx + dx[i];
        maxy = maxy + dy[i];
      }
    }
    if (ans[maxx][maxy] == 0) {
      break;
    }
  }
  maxx = 0, maxy = 0;
  while (1) {
    int tmp = ans[maxx][maxy] + 1;
    bool FLG = true;
    for (int i = 0; i < 4; i++) {
      if (maxx + dx[i] < 0) {
        continue;
      }
      if (maxy + dy[i] < 0) {
        continue;
      }
      if (tmp == ans[maxx + dx[i]][maxy + dy[i]]) {
        FLG = false;
        maxx = maxx + dx[i];
        maxy = maxy + dy[i];
      }
    }
    if (FLG) {
      break;
    }
  }
  bool FLG = true;
  for (int i = 0; i < 4; i++) {
    if (maxx + dx[i] < 0) {
      continue;
    }
    if (maxy + dy[i] < 0) {
      continue;
    }
    if (ans[maxx + dx[i]][maxy + dy[i]] < 0) {
      FLG = false;
    }
  }
  if (FLG) {
    cnt += ans[maxx][maxy];
  }
  cout << cnt << endl;
  return 0;
}
