#include <bits/stdc++.h>

using namespace std;

const int MX_SZ = 1042;
const int INF = 1e+9 + 42;

int main()
{
    ios_base::sync_with_stdio(false);
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    char a[MX_SZ][MX_SZ];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    static int sx[MX_SZ], sy[MX_SZ];
    static bool good[MX_SZ][MX_SZ];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int xu = i - 1, yu = j - 1;
            if (xu == 0)
            {
                xu = n;
            }
            if (yu == 0)
            {
                yu = m;
            }
            if ((a[i][j] == a[i][yu] and a[i][j] == a[xu][j] and a[i][j] == a[xu][yu]))
            {
                good[i][j] = true;
                sx[i]++;
            }
        }
    }
    for (int j = 1; j <= m; j++)
    {
        for (int i = 1; i <= n; i++)
        {
            int xu = i - 1, yu = j - 1;
            if (xu == 0)
            {
                xu = n;
            }
            if (yu == 0)
            {
                yu = m;
            }
            if ((a[i][j] == a[i][yu] and a[i][j] == a[xu][j] and a[i][j] == a[xu][yu]))
            {
                sy[j]++;
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int xu = i - 1, yu = j - 1;
            if (xu == 0)
            {
                xu = n;
            }
            if (yu == 0)
            {
                yu = m;
            }
            ans += (a[i][j] == a[i][yu] and a[i][j] == a[xu][j] and a[i][j] == a[xu][yu]);
        }
    }
    int mn = INF;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            mn = min(mn, sx[i] + sy[j] - good[i][j]);
        }
    }
    cout << ans - mn;
    /*
    static int sum[MX_SZ][MX_SZ];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];
            int xu = i - 1, yu = j - 1;
            if (xu == 0)
            {
                xu = n;
            }
            if (yu == 0)
            {
                yu = m;
            }
            sum[i][j] += (a[i][j] == a[i][yu] and a[i][j] == a[xu][j] and a[i][j] == a[xu][yu]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << sum[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int csum = 0;
            csum += sum[i][j];
            csum += sum[i][m] - sum[i][j];
            csum += sum[n][j] - sum[i][j];
            csum += sum[n][m] - sum[n][j] - sum[i][m] + sum[i][j];
            ans = max(ans, csum);
            cout << i << " " << j << " " << csum << endl;
        }
    }
    */
}
