#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 5000, INF = 1e6 + 7;

int a[N][N], sum[N][N], str[N][N], st[N][N];
int main()
{
    int n, k, m;
    ios_base::sync_with_stdio(0);
    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 c;
            cin >> c;
            if (c == '1')
                a[i][j] = 1;
            else
                a[i][j] = 2;
            a[i + n][j] = a[i][j];
            a[i][j + m] = a[i][j];
            a[i + n][j + m] = a[i][j];
        }
    int ans = 0;
    for (int i = 0; i < n * 2; i++)
        for (int j = 0; j < m * 2; j++)
        {
            sum[i][j] = 0;
            if (i - 1 >= 0)
                sum[i][j] += sum[i - 1][j];
            if (j - 1 >= 0)
                sum[i][j] += sum[i][j - 1];
            if (i > 0 && j > 0)
                sum[i][j] -= sum[i - 1][j - 1];
            if (j - m >= 0)
                sum[i][j] -= sum[i][j - m];
            if (i - n >= 0)
                sum[i][j] -= sum[i - n][j];
            if (j - m >= 0 && i - n >= 0)
                sum[i][j] += sum[i - n - 1][j - m - 1];

            if (j - m + 1 >= 0)
                sum[i][j] -= st[i][j - m + 1];
            if (i - n >= 0 && j - m + 1 >= 0)
                sum[i][j] += st[i - n][j - m + 1];

            if (i - n + 1 >= 0)
                sum[i][j] -= str[i - n + 1][j];
            if (i - n + 1 >= 0 && j - m >= 0)
                sum[i][j] += str[i - n + 1][j - m];

            if (i - 1 >= 0)
                st[i][j] = st[i - 1][j];
            if (j - 1 >= 0)
                str[i][j] = str[i][j - 1];
            if (i > 0 && j > 0 && a[i][j] == a[i - 1][j] && a[i - 1][j] == a[i][j - 1] && a[i][j - 1] == a[i - 1][j - 1])
            {
                sum[i][j]++;
                st[i][j]++;
                str[i][j]++;
            }
            ans = max(ans, sum[i][j]);
        }
    cout << ans << endl;
    return 0;
}
