#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

vector<char> monitor[1010];
vector<int> comfywidth;
vector<int> comfyheight;

int w, h;

void countCW(int r)
{
    int cnt = 0;
    for (int i = 0; i < h; i++)
    {
        if ((monitor[i][r] == monitor[(i + 1) % h][r]) && (monitor[(i + 1) % h][r] == monitor[(i + 1) % h][(r + 1) % w]) && (monitor[(i + 1) % h][(r + 1) % w] == monitor[i][(r + 1) % w]))
        {
            cnt++;
        }
    }
    comfywidth.push_back(cnt);
}

void countCH(int i)
{
    int cnt = 0;
    for (int r = 0; r < w; r++)
    {
        if ((monitor[i][r] == monitor[(i + 1) % h][r]) && (monitor[(i + 1) % h][r] == monitor[(i + 1) % h][(r + 1) % w]) && (monitor[(i + 1) % h][(r + 1) % w] == monitor[i][(r + 1) % w]))
        {
            cnt++;
        }
    }
    comfyheight.push_back(cnt);
}

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> h >> w;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            char a;
            cin >> a;
            monitor[i].push_back(a);
        }
    }
    long long wouldbe = 0;
    for (int i = 0; i < h; i++)
    {
        countCH(i);
        wouldbe += comfyheight[i];
    }
    for (int i = 0; i < w; i++)
    {
        countCW(i);
    }
    long long comfytotal = 0;
    for (int i = 0; i < h; i++)
    {
        for (int r = 0; r < w; r++)
        {
            long long comfynow = wouldbe;
            comfynow -= comfyheight[i];
            comfynow -= comfywidth[r];
            if ((monitor[i][r] == monitor[(i + 1) % h][r]) && (monitor[(i + 1) % h][r] == monitor[(i + 1) % h][(r + 1) % w]) && (monitor[(i + 1) % h][(r + 1) % w] == monitor[i][(r + 1) % w]))
            {
                comfynow++;
            }
            comfytotal = max(comfynow, comfytotal);
        }
    }
    cout << comfytotal << endl;
    return 0;
}
