#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline int pwm(long long xx, long long pow, int MD) {
  if (pow < 0) {
    pow = pow % (MD - 1) + MD - 1;
  }
  long long mlt = 1;
  while (pow) {
    if (pow & 1) {
      mlt *= xx;
      mlt %= MD;
    }
    xx *= xx;
    pow >>= 1;
    xx %= MD;
  }
  return (int)mlt;
}
inline long long gcdex(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long xx, yy;
  long long gc = gcdex(b, a % b, yy, xx);
  x = xx;
  y = yy - (a / b) * xx;
  return gc;
}
inline int inv(long long r, int _mod) { return pwm(r % _mod, _mod - 2, _mod); }
const int inf = 1e9;
const int MAXN = 2e5 + 10;
long long mod = 1e9 + 7;
vector<vector<int>> vert(40, vector<int>(0));
vector<string> arr;
vector<bool> was(40);
int dist(int a, int b) {
  list<pair<int, int>> list;
  list.push_back({a, 0});
  for (int i = 0; i < 40; i++) {
    was[i] = false;
  }
  was[a] = true;
  while (!list.empty()) {
    int s = list.front().first;
    int dist = list.front().second;
    if (s == b) return dist;
    list.pop_front();
    for (int son : vert[s]) {
      if (!was[son]) {
        list.push_back({son, dist + 1});
        was[son] = true;
      }
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  arr.resize(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    arr[i] = s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '#') continue;
      if (j != 0) {
        if (s[j - 1] == s[j]) {
          vert[i * m + j].push_back(i * m + j - 1);
          vert[i * m + j - 1].push_back(i * m + j);
        }
      }
      if (i != 0) {
        if (arr[i - 1][j] == s[j]) {
          vert[(i - 1) * m + j].push_back(i * m + j);
          vert[i * m + j].push_back((i - 1) * m + j);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < n; k++) {
        for (int d = 0; d < m; d++) {
          if (arr[k][d] == '#' || arr[i][j] == '#') continue;
          ans = max(dist(k * m + d, i * m + j), ans);
        }
      }
    }
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n = 1;
  for (int i = 0; i < n; i++) solve();
  return 0;
}
