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

int n, m;
int table[1001][1001];
int dpx[1001];
int dpy[1001];

int countGood(int x, int y) {
    int kol =  0;
    for (int i = x; i < n + x; i++) {
        for (int j = y; j < m + y; j++) {
            if (i + 1 >= n + x || j + 1 >= m + y) continue;
            if (table[i % n][j % m] == table[(i + 1) % n][j % m] &&
                                       table[(i + 1) % n][j % m] == table[i % n][(j + 1) % m] &&
                                                                    table[i % n][(j + 1) % m] == table[(i + 1) % n][(j + 1) % m]) kol++;
        }
    }
    return kol;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
           char q;
           cin >> q;
           table[i][j] = q - '0';
        }
    }

    for (int j = 0; j < m; j++) {
        dpy[j] = 0;
        for (int i = 0; i < n - 1; i++) {
            if (table[i][j] == table[i + 1][j] && table[i + 1][j] == table[i][(j + 1) % m] && table[i][(j + 1) % m] == table[i + 1][(j + 1) % m]) dpy[j]++;
        }
    }

    int maxi = 0;
    for (int i = 0; i < n; i++) {
        //int maxi1 = countGood(i, 0);

        for (int j = 0; j < m; j++) {
            if (i == 0) continue;
            if (table[(n + i - 2) % n][j] == table[((n + i - 2) % n + 1) % n][j] &&
                                         table[((n + i - 2) % n + 1) % n][j] == table[(n + i - 2) % n][(j + 1) % m] &&
                                                                                table[(n + i - 2) % n][(j + 1) % m] == table[((n + i - 2) % n + 1) % n][(j + 1) % m]) dpy[j]++;
            if (table[i - 1][j] == table[i][j] && table[i][j] == table[i - 1][(j + 1) % m] && table[i - 1][(j + 1) % m] == table[i][(j + 1) % m]) dpy[j]--;
        }

        int maxi1 = 0;
        for (int j = 0; j < m - 1; j++) maxi1 += dpy[j];

        //for (int i = 0; i < m; i++) cout << dpy[i] << " ";
        //cout << endl;

        int maxi2 = maxi1;
        for (int j = 1; j < m; j++) {
            maxi1 = max(maxi1, maxi2 + dpy[m - j] - dpy[m - j - 1]);
            maxi2 = maxi2 + dpy[m - j] - dpy[m - j - 1];
            //maxi1 = max(countGood(i, j), maxi1);
        }
        maxi = max(maxi, maxi1);
    }

    cout << maxi;
    return 0;
}
