#include <iostream>
#include <vector>

using namespace std;

int main()
{
    freopen("video.in", "rt", stdin);
    freopen("video.out", "wt", stdout);
    int n, m;
    cin >> n >> m;
    vector<vector<int> > mat(2 * n - 1, vector<int>(2 * m + 1));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char x;
            cin >> x;
            mat[i][j] = x - '1';
        }
    }
    for (int i = 0; i < 2 * n - 1; i++) {
        for (int j = 0; j < 2 * m - 1; j++) {
            int x = i;
            int y = j;
            if (x >= n) x -= n;
            if (y >= m) y -= m;
            mat[i][j] = mat[x][y];
        }
    }
    vector<vector<int> > ok(2 * n - 2, vector<int>(2 * m - 2));
    for (int i = 0; i < 2 * n - 2; i++) {
        for (int j = 0; j < 2 * m - 2; j++) {
            ok[i][j] = (mat[i][j] + mat[i][j + 1] + mat[i + 1][j] + mat[i + 1][j + 1]) % 4 == 0;
        }
    }
    vector<vector<int> > cnt(2 * n - 1, vector<int>(2 * m - 1));
    for (int i = 1; i < 2 * n - 1; i++) {
        for (int j = 1; j < 2 * m - 1; j++) {
            int e = ok[i - 1][j - 1];
            cnt[i][j] = cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1] + e;
        }
    }
    int ans = 0;
    for (int x1 = n - 2; x1 < 2 * n - 2; x1++) {
        for (int y1 = m - 2; y1 < 2 * m - 2; y1++) {
            int x2 = x1 - n + 2;
            int y2 = y1 - m + 2;
            int ans1 = cnt[x1 + 1][y1 + 1] - cnt[x2][y1 + 1] - cnt[x1 + 1][y2] + cnt[x2][y2];
            ans = max(ans, ans1);
        }
    }
    cout << ans << endl;
    return 0;
}
