#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(const pair<int, int> &lhs, const pair<int, int> &rhs) {
  pair<int, int> p = {lhs.first + rhs.first, lhs.second + rhs.second};
  return p;
}
bool operator==(const pair<int, int> &lhs, const pair<int, int> &rhs) {
  return (lhs.first == rhs.first) && (lhs.second == rhs.second);
}
bool is_in_field(int col, int row, const pair<int, int> &point) {
  const int c = point.second;
  const int r = point.first;
  return (0 <= c && c < col) && (0 <= r && r < row);
}
int solve(int col, int row, vector<vector<char>> &field,
          const pair<int, int> &start, const pair<int, int> &goal) {
  vector<vector<int>> memo;
  for (int i = 0; i < row; ++i) {
    vector<int> v(col, 0);
    memo.push_back(v);
  }
  const pair<int, int> points[] = {
      {-1, 0},
      {1, 0},
      {0, -1},
      {0, 1},
  };
  queue<pair<int, int>> q;
  q.push(start);
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    if (cur == goal) {
      return memo[cur.first][cur.second];
    }
    for (const auto &point : points) {
      pair<int, int> next = cur + point;
      if (is_in_field(col, row, next)) {
        const char s = field[next.first][next.second];
        if (s == '0' || s == 'g') {
          if (memo[next.first][next.second] == 0) {
            q.push(next);
            memo[next.first][next.second] = memo[cur.first][cur.second] + 1;
          }
        }
      }
    }
  }
  return -1;
}
int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> start, goal;
  vector<vector<char>> field;
  for (int i = 0; i < H; ++i) {
    vector<char> v;
    for (int j = 0; j < W; ++j) {
      char c;
      cin >> c;
      if (c == '.')
        v.push_back('0');
      else
        v.push_back('1');
    }
    field.push_back(v);
  }
  int ans = -1000000000;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < H; k++) {
        for (int l = 0; l < W; l++) {
          start.first = i;
          start.second = j;
          goal.first = k;
          goal.second = l;
          ans = max(ans, solve(W, H, field, start, goal));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
