#include <iostream>
#include <cstdio>

char mt[1000][1000];
int an[1000][1000];
int v[1000];
int h[1000];

using namespace std;

int main()
{
    freopen("video.in","r", stdin);
    freopen("video.out","w", stdout);
    int n, m, i, j;
    cin >> n >> m;
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
    {
        cin >> mt[i][j];
    }
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
    {
        if (mt[i][j] == mt[(i + 1) % n][j] &&
            mt[i][j] == mt[i][(j + 1) % n] &&
            mt[i][j] == mt[(i + 1) % n][(j + 1) % n])
                an[i][j] = 1;
        else
            an[i][j] = 0;
    }
    int sum = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            v[i] += an[i][j];
            h[j] += an[i][j];
            sum += an[i][j];
        }
    }
    int ans = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            ans = max(ans, sum - v[i] - h[j] + an[i][j]);
        }
    }
    cout << ans;
    return 0;
}
