#include <bits/stdc++.h>

using namespace std;

char c[1010][1010];
long long fir[1010];
long long sec[1010];

bool is(int x, int y) {
    if (c[x][y] == '1') {
        return 1;
    }
    return 0;
}

int main()
{
    freopen ("video.in", "r", stdin);
    freopen ("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> c[i][j];
        }
    }
    long long answer = 0;
    for (int d = 0; d < m; d++) {
        long long mi = 1e9;
        long long cur_ans = 0;
        for (int i = 0; i < n; i++) {
            long long ans = 0;
            for (int j = 0; j < m - 1; j++) {
                int y = (j + d) % m;
                int y1 = (j + d + 1) % m;
                if (c[i][y] == c[(i + 1) % n][y] && c[i][y] == c[(i + 1) % n][y1] && c[i][y] == c[i][y1]) {
                    ans++;
                }
            }
            mi = min(mi, ans);
            cur_ans += ans;
        }
        answer = max(answer, cur_ans - mi);
    }
    cout << answer << endl;
    return 0;
}
