#include <bits/stdc++.h>
using namespace std;

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

    int n, m;
    cin >> n >> m;

    vector<string> a(n);

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

    int cnt = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int p = (i+1)%n, q = (j+1)%m;
            if(a[i][j] == a[p][j] && a[i][j] == a[i][q] && a[i][j] == a[p][q])
                cnt++;
        }
    }

    int ans = 0;

    for(int di = 0; di < n; di++){
        for(int dj = 0; dj < m; dj++){
            int delta = 0;
            for(int i = 0; i < n; i++){
                int p = (di+i)%n, q = dj, p1 = (di+i+1)%n, q1 = (dj+1)%m;
                if(a[p][q] == a[p1][q] && a[p][q] == a[p][q1] && a[p][q] == a[p1][q1])
                    delta++;
            }
            for(int j = 1; j < m; j++){
                int p = di, q = (dj+j)%m, p1 = (di+1)%n, q1 = (dj+j+1)%m;
                if(a[p][q] == a[p1][q] && a[p][q] == a[p][q1] && a[p][q] == a[p1][q1])
                    delta++;
            }
            ans = max(ans, cnt-delta);
        }
    }

    cout << ans << endl;

    return 0;
}
