#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 1100;
bool mas[MAXN][MAXN] = {}, mas1[MAXN][MAXN] = {};
bool start[MAXN][MAXN] = {}, start1[MAXN][MAXN] = {};
int cur1[MAXN] = {}, cur2[MAXN] = {};
int min1 = 10000, min2 = 10000, ind1 = 0, ind2 = 0;

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	char c;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> c;
			if (c == '2')
				mas1[i][j] = 1;
		}
	}
	int ans = 0, ans1 = 0;

	for (int i2 = 0; i2 < n; i2++) {
		for (int j2 = 0; j2 < m; j2++) {
			int i1 = i2;
			int j1 = j2;
			ans = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					mas[i][j] = mas1[i1][j1];
					j1++;
					if (j1 == m) {
						j1 = 0;
					}
				}
				i1++;
				if (i1 == n) {
					i1 = 0;
				}
			}

			for (int i = 0; i < n - 1; i++) {
				for (int j = 0; j < m - 1; j++) {
					if (mas[i][j] == mas[i + 1][j] && mas[i][j] == mas[i][j + 1] && mas[i][j] == mas[i + 1][j + 1]) {
						ans++;
					}
				}
			}

			ans1 = max(ans1, ans);
		}
	}
	cout << ans1;
	return 0;
}

/*
3 3
111
121
111

2 4
1221
1221

3 3
121
212
121

3 2
22
22
22
*/