#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)

const int LEN = 1100;
string ar[LEN];
int n, m, res, dp[LEN], dp1[LEN], res1, sum;

int check(int x, int y)
{
    return (ar[x][y] == ar[(x + n - 1) % n][y] && ar[x][y] == ar[x][(y + 1) % m] && ar[x][y] == ar[(x + n - 1) % n][(y + 1) % m]);
}


int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);

    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> ar[i];

    for (int i = 0; i < n; i++)
    {
        dp[i] = 0;
        for (int j = 0; j < m - 1; j++)
            dp[i] += check(i, j);
    }

    for (int j = 0; j < m - 1; j++)
    {
        res1 = 0;
        for (int i = 1; i < n; i++)
            res1 += dp[i];

        for (int i = 1; i < n; i++)
            res = max(res, res1 - dp[i] + dp[0]);


        for (int i = 0; i < n; i++)
            dp[i] += check(i, m - j - 1) - check(i, m - j - 2);
    }


    cout << res;
    return 0;
}
