#include <bits/stdc++.h>

#define int long long
#define f first
#define s second
#define all(a) (a).begin(), (a).end()
#define len(a) (int)(a).size()
#define pb push_back
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define endl '\n'

using namespace std;

signed main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    vector<vector<bool> > a(n, vector<bool> (m));
    vector<vector<bool> > b(n, vector<bool> (m));
    forn(i, n) {
        forn(j, m) {
            char c;
            cin >> c;
            a[i][j] = c - '1';
        }
    }
    vector<int> p(n, 0);
    vector<int> s(m, 0);
    int ans = 0;
    int su = 0;
    forn(i, n) {
        forn(j, m) {
            bool a0 = a[i][j], a1, a2, a3;
            int i1 = i - 1;
            int j1 = j - 1;
            if (i1 == -1)
                i1 = n - 1;
            if (j1 == -1)
                j1 = m - 1;
            a1 = a[i][j1];
            a2 = a[i1][j];
            a3 = a[i1][j1];
            b[i][j] = (a0 == a1 && a0 == a2 && a0 == a3);
            //cout << b[i][j];
            p[i] += b[i][j];
            s[j] += b[i][j];
            su += b[i][j];
        }
        //cout << endl;
    }
    forn(i, n) {
        forn(j, m) {
            ans = max(ans, su - p[i] - s[j] + b[i][j]);
        }
    }
    cout << ans;
    return 0;
}
