#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[1003][1003];
int stb[1004], stt[1004], sum = 0, n, m;
int go(int i, int j);
int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int ed = 0, dv = 0, i, j, k, l, ans = 0, vl, kr, ost;
    //char c;
    cin >> n >> m;
    for (i = 0; i < n; i++)
    {
        scanf("\n");
        for (j = 0; j < m; j++)
        {
            scanf("%c", &a[i][j]);
        }
    }

    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
        {
            stt[i] += go(i, j);
            stb[j] += go(i, j);
        }

    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            sum += go(i, j);


   for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
        {
            vl = 0;
            vl = sum;
            vl -= stt[i];
            vl -= stb[j];
            vl += go(i, j);
            ans = max(vl, ans);
        }


    cout << ans << endl;
    return 0;
}

int go(int i, int j)
{
    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]))
        return 1;
    return 0;
}

/*

2 4
1221
1221

3 2
22
22
22

3 3
111
121
111

*/
