#include <iostream>
#include <vector>

using namespace std;

vector <vector <int>> a;

int main() {
	freopen("video.in", "r", stdin);
	freopen("video.out", "w", stdout);
	int n, m;
	cin >> n >> m;
	a.resize(n, vector<int>(m));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			char c;
			cin >> c;
			a[i][j] = c;
		}
	}
	vector <int> hor, vert;
	hor.resize(m);
	vert.resize(n);
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			if (a[j][i] == a[(j + 1) % n][i] && a[j][i] == a[j][(i + 1) % n] && a[j][i] == a[(j + 1) % n][(i + 1) % m])
				hor[i]++;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] == a[i][(j + 1) % m] && a[i][j] == a[(i + 1) % n][(j + 1) % m] && a[i][j] == a[(i + 1) % n][j])
				vert[i]++;
		}
	}
	int mni, mnj;
	int nmni, nmnj;
	nmni = nmnj = 1e9;
	for (int i = 0; i < m; i++) {
		if (hor[i] < nmni) {
			nmni = hor[i];
			mni = i;
		}
	}
	for (int i = 0; i < n; i++) {
		if (vert[i] < nmnj) {
			nmnj = vert[i];
			mnj	= i;
		}
	}
	int ans = 0;
	for (int i = mni + 1; i < mni + m; i++) {
		for (int j = mnj + 1; j < mnj + n; j++) {
			int x = i;
			int y = j;
			x %= m;
			y %= n;
			if (a[y][x] == a[(y + 1) % n][x] && a[y][x] == a[y][(x + 1) % m] && a[y][x] == a[(y + 1) % n][(x + 1) % m])
				ans++;
		}
	}
	cout << ans;
	return 0;
}