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

using namespace std;

int a[2000][2000], sum[2000][2000], n, m, b[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];
}

void sl()
{
    for (int i = 0; i < m; i++)
        b[i] = a[0][i];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
           a[i][j] = a[i + 1][j];
    for (int j = 0; j < m; j++)
        a[n - 1][j] = b[j];
}

void sr()
{
    for (int i = 0; i < m; i++)
        b[i] = a[n - 1][i];
    for (int i = n - 1; i > 0; i--)
        for (int j = 0; j < m; j++)
           a[i][j] = a[i - 1][j];
    for (int j = 0; j < m; j++)
        a[0][j] = b[j];
}


void sl1()
{
    for (int i = 0; i < n; i++)
        b[i] = a[i][0];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
           a[i][j] = a[i][j + 1];
    for (int j = 0; j < n; j++)
        a[j][m - 1] = b[j];
}

void sr1()
{
    for (int i = 0; i < n; i++)
        b[i] = a[i][m - 1];
    for (int i = 0; i < n; i++)
        for (int j = m - 1; j > 0; j--)
           a[i][j] = a[i][j - 1];
    for (int j = 0; j < n; j++)
        a[j][0] = b[j];
}

int main()
{
    int ans = 0;
    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;
        }
    for (int i = -2; i < 3; i++)
        for (int j = -2; j < 3; j++)
        {
            int i1 = i, j1= j;
            while (i1 < 0)
                sl(), i1++;
            while (i1 > 0)
                sr(), i1--;
            while (j1 < 0)
                sl1(), j1++;
            while (j1 > 0)
                sr1(), j1--;
            ans = max(ans, prove(0, n - 1, 0, m - 1));
            i1 = -i, j1 = -j;
             while (i1 < 0)
                sl(), i1++;
            while (i1 > 0)
                sr(), i1--;
            while (j1 < 0)
                sl1(), j1++;
            while (j1 > 0)
                sr1(), j1--;
        }
    cout << ans << endl;
    return 0;
}
