#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;

long long a[N][N], sum[N][N], str[N][N], st[N][N], bst[N][N], btr[N][N];

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 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;
             for (int k = 1; k < 20; k++)
                for (int k1 = 1; k1 < 20; k1++)
                a[i + n * k][j] = a[i][j], a[i][j + m * k1] = a[i][j], a[i + n * k][j + m * k1] = a[i][j];
        }
    //cout << n << ' ' << m << endl;
    long long ans = 0;
    for (int i = 0; i < n * 6; i++)
    {
        for (int j = 0; j < m * 6; j++)
        {
            sum[i][j] = 0;

            if (i > 0)
                sum[i][j] += sum[i - 1][j];
            if (j > 0)
                sum[i][j] += sum[i][j - 1];
            if (i > 0 && j > 0)
                sum[i][j] -= sum[i - 1][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]++;

            int k = sum[i][j];

            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][j - m];

            for (int k = i - n + 1; k <= i; k++)
                if (ok(k, j - m + 1)) sum[i][j]--;

            for (int k = j - m + 1; k <= j; k++)
                if (ok(i - n + 1, k)) sum[i][j]--;

            if (ok(i - n + 1, j - m + 1)) sum[i][j]++;

            //cout << i << ' ' << j << ' ' << sum[i][j] << ' ' << k << ' ' << a[i][j] << endl;
            ans = max(ans, sum[i][j]);

            sum[i][j] = k;

        }
    }
    cout << ans << endl;
    return 0;
}
