#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], bst[N][N], btr[N][N], n, m;

int ok(int i, int j)
{
    if (i > 0 && j > 0 && a[i][j] == a[i - 1][j] && a[i][j] == a[i][j - 1] && a[i][j] == a[i - 1][j - 1])
        return 1;
    return 0;
}

int prove(int i1, int j1)
{
    for (int i = 0; i < n * 2; i++)
        for (int j = 0; j < m * 2; j++)
            sum[i][j] = 0;
    for (int i = i1 - n + 1; i <= i1; i++)
        for (int j = j1 - m + 1; j <= j1; j++)
        {
            sum[i][j] = 0;
            if (j > j1 - m + 1)
                sum[i][j] += sum[i][j - 1];
            if (i > i1 - n + 1)
                sum[i][j] += sum[i - 1][j];
            if (i > i1 - n + 1 && j > j1 - m + 1)
            {
                sum[i][j] -= sum[i - 1][j - 1];
                if (a[i - 1][j - 1] == a[i - 1][j] && a[i - 1][j] == a[i][j - 1] && a[i][j - 1] == a[i][j])
                    sum[i][j]++;
            }
        }
    return sum[n][m];
}

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++)
            ans = max(ans, prove(i, j));
    }
    cout << ans << endl;
    return 0;
}
