#include <bits/stdc++.h>

using namespace std;
vector < vector <char> > a;

int main(){
    int n, m;
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> n >> m;
    a.resize(n);
    for (int i = 0; i < n; i++){
        a[i].resize(m);
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }
    vector <int> kol(n), kol1(m);
    int k = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (a[i][j] == a[(i+1)%n][j] && a[i][j] == a[(i+1)%n][(j+1)%m] && a[i][j] == a[i][(j+1)%m]){
                kol[i]++;
                kol1[j]++;
                k++;
            }
    int ans = 0;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            int b = k - kol[i] - kol1[j];
            if (a[i][j] == a[(i+1)%n][j] && a[i][j] == a[(i+1)%n][(j+1)%m] && a[i][j] == a[i][(j+1)%m]) b++;
            ans = max(ans, b);
        }
    }
    cout << ans << "\n";
    return 0;
}
