#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[2007][2007];
int dp[1007][1007];
int ans;

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

int get(int ii, int jj)
{
    int k = 0;
    for (int i = ii; i < ii + n - 1; i++)
        for (int j = jj; j < jj + m - 1; j++)
            if (ok(a[i][j], a[i + 1][j], a[i][j + 1], a[i + 1][j + 1]))
                k++;
    return k;
}



int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie();
    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];
    forn(i, n)
        forn(j, m)
        {
            a[i][j + m] = a[i][j];
            a[i + n][j] = a[i][j];
            a[i + n][j + m] = a[i][j];
        }
    /*forn(i, 2 * n)
    {
        forn(j, 2 * m)
            cout << a[i][j];
        cout << endl;
    }*/
    ans = get(0, 0);
    dp[0][0] = ans;
    forn(i, n - 1)
    {
        forn(j, m - 1)
        if (i + j != 0)
        {
            if (j == 0)
            {
                dp[i][j] = dp[i - 1][j];
                forn(jj, m - 1)
                {
                    if (ok(a[i - 1][j], a[i - 1][j + 1], a[i][j], a[i][j + 1]))
                        dp[i][j]--;
                    if (ok(a[i - 1][j], a[i - 1][j + 1], a[n - i][j], a[n - i][j + 1]))
                        dp[i][j]++;
                }
            }
            else
            {
                dp[i][j] = dp[i][j - 1];
                forn(ii, n - 1)
                {
                    if (ok(a[ii][j - 1], a[ii][j], a[ii + 1][j - 1], a[ii + 1][j]))
                        dp[i][j]--;
                    if (ok(a[ii][j - 1], a[ii][m - j], a[ii + 1][j - 1], a[ii + 1][m - j]))
                        dp[i][j]++;
                }
            }
        }
    }
    forn(i, n - 1)
        forn(j, m - 1)
            ans = max(ans, dp[i][j]);
    /*forn(i, n - 1)
    {
        forn(j, m - 1)
            cout << dp[i][j] << ' ';
        cout << endl;
    }*/
    cout << ans;

}
