#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to;
  int cost;
};
using P = pair<int, int>;
int V;
vector<edge> G[10];
int d[10];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d + V, 1e+9);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
int dp() { return 0; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  int h, w;
  cin >> h >> w;
  string s[h];
  V = 0;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int num = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.') {
        s[i][j] = 97 + num;
        num++;
      }
    }
  }
  for (int i = 0; i < h; i++) {
  }
  V = num;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') continue;
      if (i - 1 >= 0 && s[i - 1][j] != '#') {
        edge e = {(int)(s[i - 1][j] - 97), 1};
        edge f = {(int)(s[i][j] - 97), 1};
        G[(int)(s[i][j] - 97)].push_back(e);
        G[(int)(s[i - 1][j] - 97)].push_back(f);
      }
      if (j - 1 >= 0 && s[i][j - 1] != '#') {
        edge e = {(int)(s[i][j - 1] - 97), 1};
        edge f = {(int)(s[i][j] - 97), 1};
        G[(int)(s[i][j] - 97)].push_back(e);
        G[(int)(s[i][j - 1] - 97)].push_back(f);
      }
      if (i + 1 <= h - 1 && s[i + 1][j] != '#') {
        edge e = {(int)(s[i + 1][j] - 97), 1};
        edge f = {(int)(s[i][j] - 97), 1};
        G[(int)(s[i][j] - 97)].push_back(e);
        G[(int)(s[i + 1][j] - 97)].push_back(f);
      }
      if (j + 1 <= w - 1 && s[i][j + 1] != '#') {
        edge e = {(int)(s[i][j + 1] - 97), 1};
        edge f = {(int)(s[i][j] - 97), 1};
        G[(int)(s[i][j] - 97)].push_back(e);
        G[(int)(s[i][j + 1] - 97)].push_back(f);
      }
    }
  }
  int ma = 0;
  for (int j = 0; j < V; j++) {
    dijkstra(j);
    for (int i = 0; i < V; i++) {
      if (d[i] != 1e+9) {
        chmax(ma, d[i]);
      }
    }
  }
  cout << ma << endl;
  return 0;
}
