#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

char buff[1179];
char matrix[1000][1000];
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++) {
        scanf("%s", buff);
        for (int j = 0; j < m; j++) {
            matrix[i][j] = buff[j] - '0';
        }
    }
    vector<pair<int, int> > good_squares;
    vector<vector<int> > is_good(n, vector<int>(m, 0));
    int all_good = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int x1, y1, x2, y2;
            x1 = i;
            x2 = (i + 1) % n;
            y1 = j;
            y2 = (j + 1) % n;
            if (matrix[x1][y1] == matrix[x1][y2] and matrix[x1][y2] == matrix[x2][y2] and matrix[x2][y2] == matrix[x2][y1]) {
                good_squares.push_back(make_pair(x1, y1));
                is_good[x1][y1] = 1;
                all_good++;
            }
        }
    }
    vector<int> amounts(m, 0), curr_amounts;
    int ans = 0;
    for (int j = 0; j < (int)good_squares.size(); j++) {
        amounts[good_squares[j].second]++;
    }
    for (int i = 0; i < n; i++) {
        curr_amounts.assign(m, 0);
        int curr_sum = 0;
        for (int j = 0; j < m; j++) {
            if (is_good[i][j]) {
                curr_amounts[j]++;
                curr_sum++;
            }
        }
        int min_y = -1;
        for (int j = 0; j < m; j++) {
            if (min_y == -1 or amounts[min_y] - curr_amounts[min_y] > amounts[j] - curr_amounts[j]) {
                min_y = j;
            }
        }
        ans = max(ans, all_good - (amounts[min_y] - curr_amounts[min_y] + curr_sum));
    }
    cout << ans << endl;
}

