#include <bits/stdc++.h>

using namespace std;

#define filename "video"

int main() {
    ios_base::sync_with_stdio(false);

#ifndef SCHEMTSCHIK
    freopen(filename".in", "r", stdin);
    freopen(filename".out", "w", stdout);
#endif

    int n, m;
    cin >> n >> m;
    vector<string> a(n);
    for (auto& i : a)
        cin >> i;

    int ans = 0;
    for (int dn = -2; dn <= 2; dn++)
        for (int dm = -2; dm <= 2; dm++) {
            int _ans = 0;
            for (int i = 0; i < n - 1; i++)
                for (int j = 0; j < m - 1; j++) {
                    char A = a[(n + i + dn) % n][(m + j + dm) % m];
                    char B = a[(n + i + dn) % n][(m + j + dm + 1) % m];
                    char C = a[(n + i + dn + 1) % n][(m + j + dm) % m];
                    char D = a[(n + i + dn + 1) % n][(m + j + dm + 1) % m];
                    if (A == B && C == D && A == C)
                        _ans++;
                }
            ans = max(ans, _ans);
        }

    cout << ans << endl;

    return 0;
}