#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1e3 + 1;
const int INF = 2e9 + 1;

char a[N][N];

int b[N], c[N];

int main() {
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    int c1 = 0, c2 = 0;
    fr(i, n)
        fr(j, m) {
            cin >> a[i][j];
            a[i + n][j] = a[i][j + m] = a[i + n][j + m] = a[i][j];
        }

//    fr(i, n)
//        fr(j, m)
//            if (a[i][j] == a[(i + 1) % n][j] && a[i][j] == a[i][(j + 1) % m] && a[i][j] == a[(i + 1) % n][(j + 1) % m])
//                b[i]++, c[j]++;
//
//    fr(i, n)
//        cout << b[i] << " ";
//        cout << endl;
//    fr(i, m)
//        cout << c[i] << " ";cout << endl;
//    int sum = accumulate(b, b + n, 0) + accumulate(c, c + m, 0);
//    int ans = 0;
//    fr(i, n)
//        fr(j, m)
//            ans = max(ans, sum - b[i] - c[j]);
//    cout << ans;
    int ans = 0;
    fr(i1, n)
        fr(j1, m) {
            int cur = 0;
            frab(i, i1, i1 + n - 1)
                frab(j, j1, j1 + m - 1)
                    if (a[i][j] == a[(i + 1)][j] && a[i][j] == a[i][(j + 1)] && a[i][j] == a[(i + 1)][(j + 1)])
                        cur++;
            ans = max(ans, cur);
        }
    cout << ans;

}
