#include <bits/stdc++.h>

#define TASK "video"
using namespace std;
typedef long long ll;
typedef long double ld;

ll dp[1234][1234];
char mas[1234][1234];
ll v[1234];
ll h[1234];
ll n, m;
bool isg[1234][1234];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    if(strcmp(TASK,"")!=0)
    {
        freopen(TASK".in","r",stdin);
        freopen(TASK".out","w",stdout);
    }
#endif
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> mas[i][j];
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (mas[i][j] == mas[(i + 1) % n][j] && mas[i][j] == mas[i][(j + 1) % m] &&
                mas[i][j] == mas[(i + 1) % n][(j + 1) % m])
            {
                isg[i][j] = true;
                if (i != n - 1 && j != m - 1)
                    dp[0][0]++;
                v[j]++;
                h[i]++;
            }
        }
    }
    ll ans = dp[0][0];
    for (int j = 1; j < m; j++)
    {
        dp[0][j] = dp[0][j - 1] - v[j - 1] + isg[n - 1][j - 1] + v[((j - 2) % m + m) % m] -
                   isg[n - 1][((j - 2) % m + m) % m];
        ans = max(ans, dp[0][j]);
    }
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            dp[i][j] = dp[i - 1][j] - h[i - 1] + isg[i - 1][((j - 1)%m+m)%m] + h[((i - 2) % n + n) % n] -
                       isg[((i - 2) % n + n) % n][((j - 1)%m+m)%m];
            if (ans < dp[i][j])
            {
                ans = dp[i][j];
            }
        }
    }
    cout << ans;
    return 0;
}