#include <bits/stdc++.h>

using namespace std;

vector<vector<char> > pole;

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    char trash;
    scanf("%d%d", &n, &m);
    pole.resize(n + 2);
    for (int i = 0; i < n + 2; ++i)
        pole[i].resize(m + 2);
    trash = ' ';
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            scanf("%c", &trash);
            while(!(trash >= '1' && trash <= '2'))
                scanf("%c", &trash);
            pole[i][j] = trash;
            //cerr << pole[i][j] << " ";
        }
        //cerr << endl;
    }
    for (int i = 0; i <= n + 1; ++i)
    {
        pole[i][m + 1] = pole[i][1];
        pole[i][0] = pole[i][m];
    }
    for (int j = 0; j <= m + 1; ++j)
    {
        pole[n + 1][j] = pole[1][j];
        pole[0][j] = pole[n][j];
    }
    //pole[0][0] = pole[n][m];
    //pole[n + 1][m + 1] = pole[1][1];
    /**for (int i = 0; i <= n + 1; ++i)
    {
        for (int j = 0; j <= m + 1; ++j)
            cout << pole[i][j] << " ";
        cout << endl;
    }
    cout << endl;/**/
    int x = 0, y = 0;
    int ans = 0, curr = 0;
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            x = i;
            y = j;
            curr = 0;
            for (int q = i; q < n + i - 1; ++q)
            {
                for (int p = j; p < m + j - 1; ++p)
                {
                    if (pole[q][p] == pole[q + 1][p] && pole[q + 1][p] == pole[q][p + 1] && pole[q][p + 1] == pole[q + 1][p + 1])
                        curr++;
                    //cout << pole[p][q] << " ";
                }
                //cerr << q << " " << m + j - 1 << " " <<  pole[q][m + j - 1] << endl;
            }
            //cerr << i << " " << j << " " << curr << endl;
            ans = max(ans, curr);
        }
    }
    cout << ans << endl;
    return 0;
}
