#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define all(a) a.begin(), a.end()
#define re return
#define str string
#define ll long long
#define mp make_pair
#define XX first
#define YY second
#define forn(i, n) for (int i = 0; i < n; i++)

using namespace std;

int n, m;
char a[1007][1007], d[1007][1007];
char temp[10007];
int ans;

bool ok(char a, char b, char c, char d)
{
    re (a == b && a == c && a == d);
}

int get(char d[1007][1007])
{
    int k = 0;
    forn(i, n - 1)
        forn(j, m - 1)
            if (ok(d[i][j], d[i + 1][j], d[i][j + 1], d[i + 1][j + 1]))
                k++;
    /*forn(i, n)
    {
        forn(j, m)
            cout << d[i][j];
        cout << endl;
    }
    cout << endl;*/
    return k;
}

void go1(char a[1007][1007])
{
    forn(i, n)
        forn(j, m)
            d[i][j] = a[i][j];
    ans = max(ans, get(d));

    forn(sdfasdfasdf, n - 1)
    {
        forn(j, m)
            temp[j] = d[n - 1][j];
        for (int i = n - 1; i >= 0; i--)
            forn(j, m)
                d[i][j] = d[i - 1][j];
        forn(j, m)
            d[0][j] = temp[j];
        /*forn(i, n)
        {
            forn(j, m)
                cout << d[i][j];
            cout << endl;
        }
        cout << endl;*/
        ans = max(ans, get(d));
    }
}

void go2(char a[1007][1007])
{

    forn(i, n)
        forn(j, m)
            d[i][j] = a[i][j];
    ans = max(ans, get(d));

    forn(sdfasdfasdf, n - 1)
    {
        forn(j, m)
            temp[j] = d[0][j];
        for (int i = 0; i < n - 1; i++)
            forn(j, m)
                d[i][j] = d[i + 1][j];
        forn(j, m)
            d[n - 1][j] = temp[j];
        /*forn(i, n)
        {
            forn(j, m)
                cout << d[i][j];
            cout << endl;
        }
        cout << endl;*/
        ans = max(ans, get(d));
    }
}

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    //freopen("input.txt", "r", stdin);
    cin >> n >> m;
    forn(i, n)
        forn(j, m)
            cin >> a[i][j];
    ans = get(a);
    forn(sadfasdf, m)
    {
        forn(i, n)
            temp[i] = a[i][m - 1];
        forn(i, n)
            for (int j = m - 1; j >= 0; j--)
                a[i][j] = a[i][j - 1];
        forn(i, n)
            a[i][0] = temp[i];
        go1(a);
        //go2(a);

    }

    /*forn(sadfasdf, m)
    {
        forn(i, n)
            temp[i] = a[i][0];
        forn(i, n)
            for (int j = 0; j < m - 1; j++)
                a[i][j] = a[i][j + 1];
        forn(i, n)
            a[i][m - 1] = temp[i];
        go1(a);
        //go2(a);

    }*/

    cout << ans;

}
