#include <bits/stdc++.h>

using namespace std;

char mas[2000][2000];
int masx[2000][2000];
int pr[2000];
int main() {
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    char c;
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j< m; j++) {
                c = 'g';
            while(c != '1' && c != '2')
            scanf("%c", &c);
            mas[i][j] = mas[i+n][j] = mas[i][j+m] = mas[i+n][j+m]= c;
        }
      //  scanf("\n");
    }
    for(int i = 1; i < 2*n; i++) {
        for(int j = 1; j < 2*m; j++) {
            if(mas[i-1][j] == mas[i][j] && mas[i-1][j] == mas[i][j-1] && mas[i-1][j] == mas[i-1][j-1]) {
                masx[i][j] = 1;
            }
        }
    }
    for(int i = 1; i < 2*n; i++) {
        for(int j = 1; j < 2*m; j++) {
                //cout << masx[i][j];
                masx[i][j] += masx[i][j-1];
        }
        //cout << endl;
    }
    int tec = 0; int ans = 0;


    for(int i = 0; i < m; i++) {

        tec = 0;
        for(int j = 1; j < n; j++) {
            tec += masx[j][i+m-1] - masx[j][i];
        }
        //cout << tec << endl;;
        ans = max(ans, tec);
        for(int j = 0; j <= n; j++) {
            tec -= masx[j+1][i+m-1] - masx[j+1][i];
            tec += masx[j+n][i+m-1] - masx[j+n][i];
            ans = max(ans, tec);
        }
    }
    for(int i = 0; i < 2*n; i++) {
        for(int j = 0; j < 2*m; j++) {
            //cout << masx[i][j] << ' ';
        }
        //cout << endl;
    }
    printf("%d", ans);
    return 0;
}


/*

3 3
111
121
111
*/
