#include <cstdio>
#include <memory.h>
#include <cstring>
#include <vector>
#include <deque>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <functional>
#include <iostream>
#include <set>
#include <list>
#include <map>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long llu;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
const ll MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

priority_queue<int, vector<int>, greater<int> > pq;
vector<int> v;

int wmat[2001][2001];
int bmat[2001][2001];
int k;

int ge(int x1, int y1, int x2, int y2, int c) {
	if (x1 > 2 * k || y1 > 2 * k) return 0;
	if (x2 < 1 || y2 < 1) return 0;

	x1 = max(x1, 1); y1 = max(y1, 1);
	x2 = min(x2, 2 * k); y2 = min(y2, 2 * k);

	int ret = 0;
	if (c == 'W') {
		ret += wmat[x2][y2] + wmat[x1 - 1][y1 - 1];
		ret -= wmat[x2][y1 - 1] + wmat[x1 - 1][y2];
	}
	else {
		ret += bmat[x2][y2] + bmat[x1 - 1][y1 - 1];
		ret -= bmat[x2][y1 - 1] + bmat[x1 - 1][y2];
	}

	return ret;
}


int cal1(int x, int y) {
	int ret = 0;
	ret += ge(x, y, x + k - 1, y + k - 1, 'W');

	ret += ge(x + k, y + k, x + k + k - 1, y + k + k - 1, 'W');
	ret += ge(x + k, y - k, x + k + k - 1, y - k + k - 1, 'W');

	ret += ge(x - k, y + k, x - k + k - 1, y + k + k - 1, 'W');
	ret += ge(x - k, y - k, x - k + k - 1, y - k + k - 1, 'W');

	// printf("%d///", ret);

	ret += ge(x + k, y, x + k + k - 1, y + k - 1, 'B');
	ret += ge(x - k, y, x - k + k - 1, y + k - 1, 'B');

	ret += ge(x, y + k, x + k - 1, y + k + k - 1, 'B');
	ret += ge(x, y - k, x + k - 1, y - k + k - 1, 'B');

	// printf("%d///\n", ret);

	return ret;
}

int cal2(int x, int y) {
	int ret = 0;
	ret += ge(x, y, x + k - 1, y + k - 1, 'B');

	ret += ge(x + k, y + k, x + k + k - 1, y + k + k - 1, 'B');
	ret += ge(x + k, y - k, x + k + k - 1, y - k + k - 1, 'B');

	ret += ge(x - k, y + k, x - k + k - 1, y + k + k - 1, 'B');
	ret += ge(x - k, y - k, x - k + k - 1, y - k + k - 1, 'B');



	ret += ge(x + k, y, x + k + k - 1, y + k - 1, 'W');
	ret += ge(x - k, y, x - k + k - 1, y + k - 1, 'W');

	ret += ge(x, y + k, x + k - 1, y + k + k - 1, 'W');
	ret += ge(x, y - k, x + k - 1, y - k + k - 1, 'W');

	return ret;
}

int main() {
	int n;
	scanf("%d %d", &n, &k);

	int x, y;
	char c;
	for (int i = 0; i < n; i++) {
		scanf("%d %d %c", &x, &y, &c);
		if (c == 'W') wmat[x % (2 * k) + 1][y % (2 * k) + 1]++;
		else bmat[x % (2 * k) + 1][y % (2 * k) + 1]++;

		// printf("%d %d : %c\n", x % (2 * k) + 1, y % (2 * k) + 1, c);
	}

	for (int i = 1; i <= 2 * k; i++)
		for (int j = 1; j <= 2 * k; j++) {
			wmat[i][j] += wmat[i][j - 1];
			bmat[i][j] += bmat[i][j - 1];
		}

	for (int i = 1; i <= 2 * k; i++)
		for (int j = 1; j <= 2 * k; j++) {
			wmat[j][i] += wmat[j - 1][i];
			bmat[j][i] += bmat[j - 1][i];
		}

	int mav = 0;
	for (int i = 1; i <= 2 * k; i++)
		for (int j = 1; j <= 2 * k; j++) {
			mav = max(mav, cal1(i, j));
			mav = max(mav, cal2(i, j));

			// printf("%d %d : %d\n", i, j, cal1(i, j));
		}

	printf("%d", mav);
}

Measured memory_bytes: 31488