#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>

#pragma comment(linker, "/STACK:256000000")

using namespace std;

typedef long long int ll;
typedef long double ld;

const int INF = (1 << 29) + 5;
const ll LLINF = (1ll << 59) + 5;
const int MAX_N = 1005;

int n, m;
char arr[MAX_N][MAX_N];
int good[MAX_N][MAX_N];
int sums_x[MAX_N];
int sums_y[MAX_N];

int cnt_block()
{
	int answer = 0;

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			if (arr[i][j] == arr[(i + 1) % n][j] && arr[i][j] == arr[i][(j + 1) % m] && arr[i][j] == arr[(i + 1) % n][(j + 1) % m])
			{
				++answer;
				good[i][j] = 1;
			}
			else
			{
				good[i][j] = 0;
			}
		}
	}

	return answer;
}

char new_st[MAX_N];

void move_x(int go)
{
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			new_st[((go + j) % m + m) % m] = arr[i][j];
		}
		for (int j = 0; j < m; ++j)
		{
			arr[i][j] = new_st[j];
		}
	}
}

void move_y(int go)
{
	for (int i = 0; i < m; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			new_st[((go + j) % n + n) % n] = arr[j][i];
		}
		for (int j = 0; j < n; ++j)
		{
			arr[j][i] = new_st[j];
		}
	}
}

int main()
{
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);

	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; ++i)
	{
		scanf("%s", &arr[i]);
	}

	int start = cnt_block();
	int answer = 0;

	//for (int i = 0; i <= 50; ++i)
	//{
	//	for (int j = 0; j <= 50; ++j)
	//	{
	//		move_x(i);
	//		move_y(j);
	//		answer = max(answer, cnt_block());
	//		move_x(-i);
	//		move_y(-j);
	//	}
	//}

	for (int i = 0; i < n; ++i)
	{
		sums_x[i] = 0;
		for (int j = 0; j < m; ++j)
		{
			sums_x[i] += good[i][j];
		}
	}

	for (int i = 0; i < m; ++i)
	{
		sums_y[i] = 0;
		for (int j = 0; j < n; ++j)
		{
			sums_y[i] += good[j][i];
		}
	}

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			answer = max(answer, start - sums_x[i] - sums_y[j] + good[i][j]);
		}
	}

	printf("%d", answer);

	fclose(stdin);
	fclose(stdout);

	return 0;
}