#include <bits/stdc++.h>
using namespace std;
char field[1000][1000];
int n, m;
int hor[1000], ver[1000];
bool check(int i, int j)
{
    return (field[i][j] == field[i][(j + 1) % m] && field[(i + 1) % n][j] == field[(i + 1) % n][(j + 1) % m] && field[i][j] == field[(i + 1) % n][j]);
}
int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            char ch;
            cin >> ch;
            field[i][j] = ch;
        }
    }
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (check(i, j))
            {
                hor[i]++;
                ver[j]++;
                cnt++;
            }
        }
    }
    int mx = -1;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            mx = max(mx, cnt - hor[i] - ver[j] + (int)check(i, j));
        }
    }
    cout << mx << endl;
    return 0;
}
