#include <bits/stdc++.h>

using namespace std;

#define filename "video"

int main() {
    ios_base::sync_with_stdio(false);

#ifndef SCHEMTSCHIK
    freopen(filename".in", "r", stdin);
    freopen(filename".out", "w", stdout);
#endif

    int n, m;
    cin >> n >> m;
    vector<string> a(n);
    for (auto& i : a)
        cin >> i;

    vector<int> dn(n, 0);
    vector<int> dm(m, 0);
    vector< vector<int> > d(n, vector<int>(m, 0));

    int ans = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (a[i][j] == a[i][(j + 1) % m] && a[i][j] == a[(i + 1) % n][j] && a[(i + 1) % n][j] == a[(i + 1) % n][(j + 1) % m]) {
                ans++;
                dn[i]++;
                dm[j]++;
                d[i][j]++;
            }
        }

    int tmp = (int)1e9;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            tmp = min(tmp, dn[i] + dm[j] - d[i][j]);

    cout << ans - tmp << endl;

    return 0;
}