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

using namespace std;

int a[2000][2000], sum[2000][2000];

int prove(int l, int n, int r, int m)
{
    for (int i = l; i <= n; i++)
    {
        for (int j = r; j <= m; j++)
        {
            //cout << a[i][j];
            sum[i][j] = 0;
            if (j > r)
                sum[i][j] += sum[i][j - 1];
            if (i > l)
                sum[i][j] += sum[i - 1][j];
            if (i > l && j > r)
            {
                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]++;
            }
        }
        //cout << endl;
    }
    //cout << sum[n][m] << endl;
    return sum[n][m];
}


int main()
{
    int n, m, ans = 0;
    ios_base::sync_with_stdio(0);
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            char c;
            cin >> c;
            if (c == '1')
                a[i][j] = 1;
            else
                a[i][j] = 2;
        }
    for (int i = 1; i <= m; i++)
        a[0][i] = a[n][i], a[n + 1][i] = a[1][i];
    for (int j = 0; j <= n + 1; j++)
        a[j][0] = a[j][m], a[j][m + 1] = a[j][1];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            int l, r, l1, r1;
            if (i == 0)
                l = 0, r = n - 1;
            if (i == 1)
                l = 1, r = n;
            if (i == 2)
                l = 2, r = n + 1;

            if (j == 0)
                l1 = 0, r1 = m - 1;
            if (j == 1)
                l1 = 1, r1 = m;
            if (j == 2)
                l1 = 2, r1 = m + 1;
            ans = max(ans, prove(l, r, l1, r1));
        }
    cout << ans << endl;
    return 0;
}
