#include <bits/stdc++.h>

using namespace std;

int main() {
    const int MAX_N = 100, MAX_M = 100;
    int n, m, matrix[MAX_N][MAX_M], countGoodRow[MAX_N], countGoodCol[MAX_M], countGood;
    bool isGood[MAX_N][MAX_M];
    string s;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> s;
        for (int j = 0; j < m; j++) {
            if (s[j] == '1') {
                matrix[i][j] = 1;
            }
            if (s[j] == '2') {
                matrix[i][j] = 2;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (matrix[i][j] == matrix[(i + 1) % n][j] && matrix[i][j] == matrix[i][(j + 1) % m] && matrix[i][j] == matrix[(i + 1) % n][(j + 1) % m]) {
                isGood[i][j] = true;
//                cout << i << ' ' << j << endl;
            }
        }
    }
    countGood = 0;
    for (int i = 0; i < n; i++) {
        countGoodRow[i] = 0;
    }
    for (int j = 0; j < m; j++) {
        countGoodCol[j] = 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            countGoodRow[i] += isGood[i][j];
            countGoodCol[j] += isGood[i][j];
            countGood += isGood[i][j];
        }
    }
//    cout << countGood << endl;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans = max(ans, countGood - (countGoodRow[i] + countGoodCol[j] - isGood[i][j]));
        }
    }
    cout << ans << endl;
    return 0;
}
