#include <bits/stdc++.h>

using namespace std;

string s[1001];
int mas[1001][1001], col1[1001], col2[1001];

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin.sync_with_stdio(false);
    cout.sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            mas[i][j] = 0;
            col2[j] = 0;
        }
        col1[i] = 0;
        cin >> s[i];
    }
    int x1 = 0, x2 = 1, y1 = 0, y2 = 1;
    int ans = 0;
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (s[x1][y1] == s[x1][y2] && s[x1][y1] == s[x2][y1] && s[x1][y1] == s[x2][y2])
            {
                mas[x2][y2]++;
                col1[x2]++;
                col2[y2]++;
                sum++;
            }
            y1++;
            y2++;
            if (y1 == m)
                y1 = 0;
            if (y2 == m)
                y2 = 0;
        }
        x1++;
        x2++;
        if (x1 == n)
            x1 = 0;
        if (x2 == n)
            x2 = 0;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            ans = max(ans, sum - col1[i] - col2[j] + mas[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}
