#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
vector<vector<bool> > mp;


int n, m;

int solve(int a, int b)
{
	int ans = 0;
	for (int i = 0; i < n - 1; ++i)
	{
		int x = (a + i) % n;
		int x2 = (a + i + 1) % n;
		for (int j = 0; j < m - 1; ++j)
		{
			int y = (b + j) % m;
			int y2 = (b + j + 1) % m;
			if ((mp[x][y] == mp[x2][y]) && (mp[x][y] == mp[x][y2]) && (mp[x][y] == mp[x2][y2]))
			ans++;
		}
	}
	return ans;
}

int main()
{
	cin >> n >> m;
	mp.resize(n);
	for (int i = 0; i < n; ++i)
	{
		mp[i].resize(m);
		for (int j = 0; j < m; ++j)
		{
			char t;
			cin >> t;
			mp[i][j] = (t == '2');
		}
	}
	
	int mm = -1;
	
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			mm = max(solve(i, j), mm);
		}
	}
	
	cout << mm << endl;
	
	return 0;	
}
