#include <bits/stdc++.h>

using namespace std;

string s[2000];

int gor[1000];
int ver[1000];

int main()
{
    ifstream cin("video.in");
    ofstream cout("video.out");
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++){
        cin >> s[i];
    }

    int ans = 0;
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < m - 1; j++)
            ans += s[i][j] == s[i + 1][j] && s[i][j] == s[i][j + 1] && s[i][j] == s[i + 1][j + 1];



    for (int i = 0; i < n; i++){
        s[i] += s[i];
        s[i + n] = s[i];
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m - 1; j++)
            ver[i] += s[i][j] == s[(i + 1) % n][j] && s[i][j] == s[i][j + 1] && s[i][j] == s[(i + 1) % n][j + 1];

    for (int j = 0; j < m; j++)
        for (int i = 0; i < n - 1; i++)
            gor[i] += s[i][j] == s[i + 1][j] && s[i][j] == s[i][(j + 1) % m] && s[i][j] == s[i + 1][(j + 1) % m];
    int now = ans;
    int ii = 0, jj = 0;

    int t = 1;
    for (int kek = 0; kek < (n + 1) * (m + 1) - 1; kek++){
        if (t == 1 && jj != m){
            now -= gor[jj - 1];
            now += gor[jj + m - 1];
            jj++;
            ans = max(ans, now);
            continue;
        }

        if (t == -1 && jj != 0){
            now += gor[jj - 1];
            now += gor[jj + m - 2];
            jj--;
            ans = max(ans, now);
            continue;
        }

        now += ver[ii + n - 1];
        now -= ver[ii];

        if (t == 1)
            t = -1;
        else
            t = 1;
        ii++;
        ans = max(ans, now);
        continue;

    }


    cout << ans;
    return 0;
}
