#pragma comment(linker, "/Stack:512000000")
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long LL;
typedef double dbl;

const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const dbl EPS = 1e-9;

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#elif 1
# define PROB "video"
  freopen(PROB".in", "r", stdin);
  freopen(PROB".out", "w", stdout);
#endif
  cin.tie(0);
  cin.sync_with_stdio(0);

  int n, m;
  cin >> n >> m;

  const int c = 10;

  unordered_map<int, unordered_map<int, char>> g;

  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < m; ++j) {
      g[i][j] = s[j];
    }

    for (int j = 0; j < c; ++j)
      g[i][m + j] = g[i][j];

    for (int j = 0; j > -c; --j)
      g[i][j] = g[i][m - 1 + j];

  }

  for (int i = 0; i < c; ++i)
    g[n + i] = g[i];

  for (int i = 0; i < c; ++i)
    g[-i] = g[n - i - 1];

  int ans = 0;

  for (int dx = -c + 1; dx < c; ++dx) {
    for (int dy = -c + 1; dy < c; ++dy) {
      int cur = 0;
      for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < m - 1; ++j) {
          char c = g[i + dx][j + dy];
          if (c == g[i + 1 + dx][j + dy] && c == g[i + dx][j + dy + 1] && c == g[i + dx + 1][j + dy + 1])
            cur++;
        }
      }
      ans = max(ans, cur);
    }
  }

  cout << ans << endl;

  return 0;
}