AtCoder Beginner Contest 086 - Checker

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:
AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

-----Constraints-----
 - 1 ≤ N ≤ 10^5
 - 1 ≤ K ≤ 1000
 - 0 ≤ x_i ≤ 10^9
 - 0 ≤ y_i ≤ 10^9
 - If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).
 - c_i is B or W.
 - N, K, x_i and y_i are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

-----Output-----
Print the maximum number of desires that can be satisfied at the same time.

-----Sample Input-----
4 3
0 1 W
1 2 W
5 3 B
5 4 B

-----Sample Output-----
4

He can satisfy all his desires by painting as shown in the example above.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
ws = [[0] * K for _ in range(K)]
bs = [[0] * K for _ in range(K)]
for _ in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	t = x // K + y // K
	if t % 2:
		if c == 'W':
			c = 'B'
		elif c == 'B':
			c = 'W'
	ws[y % K][x % K] += int(c == 'W')
	bs[y % K][x % K] += int(c == 'B')
csw = [[0] * (K + 1) for _ in range(K + 1)]
csb = [[0] * (K + 1) for _ in range(K + 1)]
for i in range(K):
	for j in range(K):
		csw[i + 1][j + 1] = csw[i + 1][j] + csw[i][j + 1] - csw[i][j] + ws[i][j]
		csb[i + 1][j + 1] = csb[i + 1][j] + csb[i][j + 1] - csb[i][j] + bs[i][j]
res = 0
for i in range(K + 1):
	for j in range(K + 1):
		u = csw[-1][-1] - csw[i][-1] - csw[-1][j] + csw[i][j] * 2
		v = csb[i][-1] + csb[-1][j] - csb[i][j] * 2
		res = max(res, u + v)
		u = csb[-1][-1] - csb[i][-1] - csb[-1][j] + csb[i][j] * 2
		v = csw[i][-1] + csw[-1][j] - csw[i][j] * 2
		res = max(res, u + v)
print(res)


(n, k) = map(int, input().split())
black = 0
imos = [[0 for i in range(k)] for j in range(k)]
for i in range(n):
	(X, Y, C) = input().split()
	x = int(X)
	y = int(Y)
	c = 1 if C == 'W' else 0
	x = x % (2 * k)
	y = y % (2 * k)
	if x > k - 1:
		x -= k
		c += 1
	if y > k - 1:
		y -= k
		c += 1
	c = 1 if c % 2 == 1 else -1
	imos[x][y] += c
	if c == -1:
		black += 1
for i in range(k):
	for j in range(k - 1):
		imos[i][j + 1] += imos[i][j]
for j in range(k):
	for i in range(k - 1):
		imos[i + 1][j] += imos[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		m = imos[k - 1][k - 1] - imos[k - 1][j] - imos[i][k - 1] + 2 * imos[i][j]
		ans = max(ans, max(n - (black + m), black + m))
print(ans)


import itertools
import numpy as np
(N, K, *XYC) = open(0).read().split()
N = int(N)
K = int(K)
board = np.zeros((4 * K + 1, 4 * K + 1), dtype=np.uint16)
for (x, y, c) in zip(XYC[::3], XYC[1::3], XYC[2::3]):
	x = int(x)
	y = int(y)
	if c == 'W':
		x += K
	x = x % (2 * K)
	y = y % (2 * K)
	board[x, y] += 1
	board[(x + K) % (2 * K), (y + K) % (2 * K)] += 1
board[1 + 2 * K:, 1 + 2 * K:] = board[:2 * K, :2 * K]
board[1:1 + 2 * K, 1 + 2 * K:] = board[1 + 2 * K:, 1 + 2 * K:]
board[1 + 2 * K:, 1:1 + 2 * K] = board[1 + 2 * K:, 1 + 2 * K:]
board[1:1 + 2 * K, 1:1 + 2 * K] = board[1 + 2 * K:, 1 + 2 * K:]
board[0, :] = 0
board[:, 0] = 0
for j in range(1, 1 + 4 * K):
	board[:, j] += board[:, j - 1]
for j in range(1, 1 + 4 * K):
	board[j, :] += board[j - 1, :]
ans = np.max(board[K:3 * K, K:3 * K] + board[:2 * K, :2 * K] - board[:2 * K, K:3 * K] - board[K:3 * K, :2 * K])
print(ans)


import sys
import numpy as np
from collections import Counter
read = sys.stdin
(N, K) = list(map(int, read.readline().split()))
k = 2 * K
xyc = np.array(read.read().split(), np.str).reshape(-1, 3)
x = xyc[:, 0].astype(int)
y = xyc[:, 1].astype(int)
c = xyc[:, 2]
x[c == 'B'] -= K
x %= k
y %= k
xy = Counter(list(zip(x, y)))
cnt = np.array(list(xy.values()), np.int64)
xy = np.array(list(xy.keys()), np.int64)
x = xy[:, 0]
y = xy[:, 1]
grid = np.zeros((3 * K, 3 * K), np.int64)
grid[x, y] += cnt
grid[x + K, y + K] += cnt
grid[x + K, y] -= cnt
grid[x, y + K] -= cnt
np.cumsum(grid, axis=0, out=grid)
np.cumsum(grid, axis=1, out=grid)
p1 = grid[:K, :K] + grid[k:, k:] + grid[K:k, K:k] + grid[k:, :K] + grid[:K, k:]
p2 = grid[K:k, :K] + grid[K:k, k:] + grid[:K, K:k] + grid[k:, K:k]
print(max(p1.max(), p2.max()))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
from collections import deque
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def query_cumsum2d(s, x1, y1, x2, y2):
	if x2 < x1:
		(x1, x2) = (x2, x1)
	if y2 < y1:
		(y1, y2) = (y2, y1)
	return s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1]

def main():
	(N, K) = LI()
	G = [[0 for _ in range(2 * K)] for __ in range(2 * K)]
	cumsum2d = [[0 for _ in range(2 * K + 1)] for __ in range(2 * K + 1)]
	xyc = []
	for _ in range(N):
		(x, y, c) = LS()
		x = int(x)
		y = int(y)
		if c == 'W':
			x += K
		x = x % (2 * K)
		y = y % (2 * K)
		G[x][y] += 1
		xyc.append((x, y, c))
	for x in range(2 * K):
		for y in range(2 * K):
			cumsum2d[x + 1][y + 1] = cumsum2d[x + 1][y] + cumsum2d[x][y + 1] - cumsum2d[x][y] + G[x][y]
	ans = 0
	for x in range(K):
		for y in range(K):
			cnt = query_cumsum2d(cumsum2d, 0, 0, x, y) + query_cumsum2d(cumsum2d, x, y, x + K, y + K) + query_cumsum2d(cumsum2d, x + K, 0, 2 * K, y) + query_cumsum2d(cumsum2d, 0, y + K, x, 2 * K) + query_cumsum2d(cumsum2d, 2 * K, 2 * K, x + K, y + K)
			ans = max(ans, cnt, N - cnt)
	print(ans)
main()


import sys
readline = sys.stdin.readline
(N, K) = map(int, readline().split())
white = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
black = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
white_all = 0
black_all = 0
for i in range(N):
	(x, y, c) = readline().split()
	(x, y) = (int(x) % (2 * K), int(y) % (2 * K))
	if c == 'W':
		white[x + 1][y + 1] += 1
		white_all += 1
	else:
		black[x + 1][y + 1] += 1
		black_all += 1
for i in range(1, len(white)):
	for j in range(1, len(white[i])):
		white[i][j] += white[i][j - 1]
for j in range(1, len(white)):
	for i in range(1, len(white[j])):
		white[i][j] += white[i - 1][j]
for i in range(1, len(black)):
	for j in range(1, len(black[i])):
		black[i][j] += black[i][j - 1]
for j in range(1, len(black)):
	for i in range(1, len(black[j])):
		black[i][j] += black[i - 1][j]
cases = ((0, 0), (K, K), (-K, -K), (K, -K), (-K, K))
ans = 0
for i in range(1, K + 1):
	for j in range(1, K + 1):
		black_cnt = 0
		white_cnt = 0
		for c in cases:
			x1 = i + c[0]
			y1 = j + c[1]
			x2 = x1 + K - 1
			y2 = y1 + K - 1
			x1 = max(1, x1)
			y1 = max(1, y1)
			x2 = min(2 * K, x2)
			y2 = min(2 * K, y2)
			if x1 > x2 or y1 > y2:
				continue
			black_cnt += black[x2][y2] - black[x2][y1 - 1] - black[x1 - 1][y2] + black[x1 - 1][y1 - 1]
			white_cnt += white[x2][y2] - white[x2][y1 - 1] - white[x1 - 1][y2] + white[x1 - 1][y1 - 1]
		point = max(black_cnt + (white_all - white_cnt), white_cnt + (black_all - black_cnt))
		if ans < point:
			ans = point
print(ans)


(N, K) = map(int, input().split())
A = [[0] * (2 * K) for j in range(2 * K)]
for i in range(N):
	(x, y, a) = input().split()
	(x, y) = (int(x), int(y))
	if a == 'B':
		y += K
	x = x % (2 * K)
	y = y % (2 * K)
	A[y][x] += 1

def da_generate(h, w, a):
	da = [[0] * w for j in range(h)]
	da[0][0] = a[0][0]
	for i in range(1, w):
		da[0][i] = da[0][i - 1] + a[0][i]
	for i in range(1, h):
		cnt_w = 0
		for j in range(w):
			cnt_w += a[i][j]
			da[i][j] = da[i - 1][j] + cnt_w
	return da

def da_calc(p, q, x, y):
	if p > x or q > y:
		return 0
	if p == 0 and q == 0:
		return da[x][y]
	if p == 0:
		return da[x][y] - da[x][q - 1]
	if q == 0:
		return da[x][y] - da[p - 1][y]
	return da[x][y] - da[p - 1][y] - da[x][q - 1] + da[p - 1][q - 1]
f = 2 * K
da = da_generate(f, f, A)
ans = 0
for i in range(K + 1):
	for j in range(K + 1):
		a = da_calc(j, i, j + K - 1, i + K - 1)
		(y1, y2, x1, x2) = (i, i + K - 1, j, j + K - 1)
		if x1 > 0 and y1 > 0:
			a += da_calc(0, 0, x1 - 1, y1 - 1)
		if x1 > 0 and y2 < f:
			a += da_calc(0, y2 + 1, x1 - 1, f - 1)
		if x2 < f and y1 > 0:
			a += da_calc(x2 + 1, 0, f - 1, y1 - 1)
		if x2 < f and y2 < f:
			a += da_calc(x2 + 1, y2 + 1, f - 1, f - 1)
		ans = max(ans, a, N - a)
print(ans)


(N, K) = map(int, input().split())
cansub = [[] for i in range(2 * K)]
notsub = [[] for i in range(2 * K)]
notlis = [0] * (2 * K)
canlis = [0] * (2 * K)
for i in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	x %= 2 * K
	y %= 2 * K
	if x >= K and y >= K or (x < K and y < K):
		nc = 'W'
	else:
		nc = 'B'
	if nc == c:
		cansub[x].append(y)
		canlis[y] += 1
	else:
		notsub[x].append(y)
		notlis[y] += 1
ans = 0
for i in range(2 * K):
	ncan = sum(canlis)
	for j in range(2 * K):
		j %= 2 * K
		ans = max(ans, ncan)
		ncan += notlis[j]
		ncan -= canlis[j]
		t = notlis[j]
		notlis[j] = canlis[j]
		canlis[j] = t
		j += K
		j %= 2 * K
		ncan += notlis[j]
		ncan -= canlis[j]
		t = notlis[j]
		notlis[j] = canlis[j]
		canlis[j] = t
	for k in cansub[i]:
		notlis[k] += 1
		canlis[k] -= 1
	for k in notsub[i]:
		notlis[k] -= 1
		canlis[k] += 1
	t = cansub[i]
	cansub[i] = notsub[i]
	notsub[i] = t
	i += K
	i %= 2 * K
	for k in cansub[i]:
		notlis[k] += 1
		canlis[k] -= 1
	for k in notsub[i]:
		notlis[k] -= 1
		canlis[k] += 1
	t = cansub[i]
	cansub[i] = notsub[i]
	notsub[i] = t
print(ans)


(N, K) = map(int, input().split())
import numpy as np
(_2K, _4K) = (2 * K, 4 * K)
shape = (_2K + 1, _4K + 1)
Grd = np.zeros(shape, dtype='int64')
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'B':
		(x, y) = (x % _2K, y % _2K)
	else:
		(x, y) = ((x + K) % _2K, y % _2K)
	if x >= K and y >= K:
		x -= K
		y -= K
	if x < K and y >= K:
		x += K
		y -= K
	Grd[y, x] += 1
	Grd[y + K, x + K] += 1
	Grd[y + K, x] += -1
	Grd[y, x + K] += -1
del x, y, c
Grd = Grd.cumsum(axis=1).cumsum(axis=0)
Grd = Grd[:, :_2K] + Grd[:, _2K:_4K]
Grd = Grd[:K, :] + np.concatenate((Grd[K:_2K, K:_2K], Grd[K:_2K, :K]), axis=1)
ans = Grd.max()
print(ans)


import numpy as np
(N, K) = list(map(int, input().split()))
ub = K * 2
b = [[0 for j in range(ub + 1)] for i in range(ub + 1)]
ADD = {'B': 0, 'W': K}

def create_bound(p, K, ub):
	if p - K + 1 >= 0:
		return [(p - K + 1, p + 1)]
	else:
		return [(0, p + 1), ((p - K + 1) % ub, ub)]
for i in range(N):
	(x, y, c) = input().split()
	nx = int(x) % ub
	ny = (int(y) + ADD[c]) % ub
	for add in [0, K]:
		for (xlb, xub) in create_bound((nx + add) % ub, K, ub):
			for (ylb, yub) in create_bound((ny + add) % ub, K, ub):
				b[xlb][ylb] += 1
				b[xlb][yub] -= 1
				b[xub][ylb] -= 1
				b[xub][yub] += 1
b_arr = np.array(b)
for i in range(ub):
	b_arr[i + 1, :] += b_arr[i, :]
for j in range(ub):
	b_arr[:, j + 1] += b_arr[:, j]
print(b_arr.max())


import sys
import numpy as np
read = sys.stdin
(N, K) = list(map(int, read.readline().split()))
k = 2 * K
xyc = np.array(read.read().split(), np.str)
x = xyc[::3].astype(int)
y = xyc[1::3].astype(int)
c = xyc[2::3]
x[c == 'B'] -= K
x %= k
y %= k
grid = np.zeros((3 * K, 3 * K), np.int64)
np.add.at(grid, (x, y), 1)
np.add.at(grid, (x + K, y + K), 1)
np.subtract.at(grid, (x + K, y), 1)
np.subtract.at(grid, (x, y + K), 1)
np.cumsum(grid, axis=0, out=grid)
np.cumsum(grid, axis=1, out=grid)
p1 = grid[:K, :K] + grid[k:, k:] + grid[K:k, K:k] + grid[k:, :K] + grid[:K, k:]
p2 = grid[K:k, :K] + grid[K:k, k:] + grid[:K, K:k] + grid[k:, K:k]
print(max(p1.max(), p2.max()))


from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
from heapq import *
import copy
(N, K) = map(int, input().split())
XYC = [input().split() for i in range(N)]
for i in range(N):
	(x, y, c) = XYC[i]
	XYC[i] = [int(x), int(y), c]
lstB = [[0 for i in range(K)] for m in range(K)]
lstW = [[0 for i in range(K)] for m in range(K)]
for (x, y, c) in XYC:
	if (x // K + y // K + (c == 'W') * 1) % 2 == 0:
		lstB[x % K][y % K] += 1
	else:
		lstW[x % K][y % K] += 1
sumB = [sum(lstB[x]) for x in range(K)]
sumW = [sum(lstW[x]) for x in range(K)]
ruisumB = [0]
ruisumW = [0]
for i in range(0, K):
	ruisumB.append(ruisumB[i] + sumB[i])
	ruisumW.append(ruisumW[i] + sumW[i])
ruiBy = [[0] for k in range(K)]
ruiWy = [[0] for k in range(K)]
for y in range(K):
	for x in range(K):
		ruiBy[y].append(lstB[x][y] + ruiBy[y][x])
		ruiWy[y].append(lstW[x][y] + ruiWy[y][x])
Revalue = 0
for x in range(K + 1):
	value = 0
	value += ruisumW[x]
	value += ruisumB[K] - ruisumB[x]
	Revalue = max(Revalue, value)
	for y in range(K):
		value += ruiWy[y][K] - 2 * ruiWy[y][x]
		value -= ruiBy[y][K] - 2 * ruiBy[y][x]
		Revalue = max(Revalue, value)
for x in range(K + 1):
	value = 0
	value += ruisumB[x]
	value += ruisumW[K] - ruisumW[x]
	Revalue = max(Revalue, value)
	for y in range(K):
		value += ruiBy[y][K] - 2 * ruiBy[y][x]
		value -= ruiWy[y][K] - 2 * ruiWy[y][x]
		Revalue = max(Revalue, value)
print(Revalue)


def calc(lx, ly, rx, ry):
	if rx < 0 or ry < 0 or lx > rx or (ly > ry):
		return 0
	ret = acum[ry * 2 * k + rx]
	if lx != 0:
		ret -= acum[ry * 2 * k + lx - 1]
	if ly != 0:
		ret -= acum[(ly - 1) * 2 * k + rx]
	if lx != 0 and ly != 0:
		ret += acum[(ly - 1) * 2 * k + lx - 1]
	return ret
(n, k) = map(int, input().split())
board = [0] * (4 * k * k)
acum = [0] * (4 * k * k)
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x) % (2 * k), int(y) % (2 * k))
	if c == 'B':
		board[y * 2 * k + x] += 1
	if c == 'W':
		y += k
		y %= 2 * k
		board[y * 2 * k + x] += 1
for i in range(2 * k):
	for j in range(2 * k):
		acum[i * 2 * k + j] = board[i * 2 * k + j]
		if i != 0:
			acum[i * 2 * k + j] += acum[(i - 1) * 2 * k + j]
		if j != 0:
			acum[i * 2 * k + j] += acum[i * 2 * k + j - 1]
		if i != 0 and j != 0:
			acum[i * 2 * k + j] -= acum[(i - 1) * 2 * k + j - 1]
ans = 0
for i in range(k):
	for j in range(k):
		tmp1 = 0
		(lx, ly) = (i, j)
		(rx, ry) = (lx + k - 1, ly + k - 1)
		tmp1 += calc(lx, ly, rx, ry)
		(tlx, tly) = (0, 0)
		(trx, ttry) = (lx - 1, ly - 1)
		tmp1 += calc(tlx, tly, trx, ttry)
		(tlx, tly) = (rx + 1, 0)
		(trx, ttry) = (2 * k - 1, ly - 1)
		tmp1 += calc(tlx, tly, trx, ttry)
		(tlx, tly) = (0, ry + 1)
		(trx, ttry) = (lx - 1, 2 * k - 1)
		tmp1 += calc(tlx, tly, trx, ttry)
		(tlx, tly) = (rx + 1, ry + 1)
		(trx, ttry) = (2 * k - 1, 2 * k - 1)
		tmp1 += calc(tlx, tly, trx, ttry)
		tmp2 = 0
		(tlx, tly) = (lx, 0)
		(trx, ttry) = (rx, ly - 1)
		tmp2 += calc(tlx, tly, trx, ttry)
		(tlx, tly) = (0, ly)
		(trx, ttry) = (lx - 1, ry)
		tmp2 += calc(tlx, tly, trx, ttry)
		(tlx, tly) = (rx + 1, ly)
		(trx, ttry) = (2 * k - 1, ry)
		tmp2 += calc(tlx, tly, trx, ttry)
		(tlx, tly) = (lx, ry + 1)
		(trx, ttry) = (rx, 2 * k - 1)
		tmp2 += calc(tlx, tly, trx, ttry)
		ans = max(ans, tmp1, tmp2)
print(ans)


(N, K) = list(map(int, input().split()))
B = [[0] * (2 * K) for i in range(2 * K)]
W = [[0] * (2 * K) for i in range(2 * K)]
for i in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x) % (2 * K), int(y) % (2 * K))
	if c == 'B':
		B[x][y] += 1
	else:
		W[x][y] += 1
BS = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
WS = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
for i in range(2 * K):
	for j in range(2 * K):
		BS[i + 1][j + 1] = BS[i][j + 1] + BS[i + 1][j] - BS[i][j] + B[i][j]
		WS[i + 1][j + 1] = WS[i][j + 1] + WS[i + 1][j] - WS[i][j] + W[i][j]

def BOX(x1, y1, x2, y2, S):
	return S[x2][y2] - S[x1][y2] - S[x2][y1] + S[x1][y1]

def smax(a, b):
	if a < b:
		return b
	else:
		return a
ans = 0
for i in range(K):
	for j in range(K):
		count = 0
		count += BOX(0, 0, i, j, WS) + BOX(i, 0, i + K, j, BS) + BOX(i + K, 0, 2 * K, j, WS)
		count += BOX(0, j, i, j + K, BS) + BOX(i, j, i + K, j + K, WS) + BOX(i + K, j, 2 * K, j + K, BS)
		count += BOX(0, j + K, i, 2 * K, WS) + BOX(i, j + K, i + K, 2 * K, BS) + BOX(i + K, j + K, 2 * K, 2 * K, WS)
		ans = smax(smax(count, N - count), ans)
print(ans)


printn = lambda x: print(x, end='')
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
ins = lambda : input().strip()
DBG = True
BIG = 10 ** 18
R = 10 ** 9 + 7

def ddprint(x):
	if DBG:
		print(x)
(n, k) = inm()
k2 = k * 2
a = [[0] * k2 for i in range(k2)]
for i in range(n):
	(x, y, c) = input().split()
	xx = int(x) % k2
	yy = (int(y) + (k if c == 'B' else 0)) % k2
	a[xx][yy] += 1
racc = [[0] * k2 for i in range(k2)]
rx = [[0] * k2 for i in range(k2)]
for i in range(k2):
	for j in range(k2):
		racc[i][j] = (0 if j == 0 else racc[i][j - 1]) + a[i][j]
	rsum = sum(a[i])
	for j in range(k):
		rx[i][j] = racc[i][j + k - 1] - (0 if j == 0 else racc[i][j - 1])
	for j in range(k, k2):
		rx[i][j] = rsum - rx[i][j - k]
cacc = [[0] * k2 for i in range(k2)]
cx = [[0] * k2 for i in range(k2)]
for i in range(k2):
	for j in range(k2):
		cacc[j][i] = (0 if j == 0 else cacc[j - 1][i]) + rx[j][i]
if False and DBG:
	for i in range(k2):
		ddprint(a[i])
	ddprint('')
	for i in range(k2):
		ddprint(rx[i])
	ddprint('')
	for i in range(k2):
		ddprint(cacc[i])
mx = 0
for i in range(k2):
	for j in range(k2):
		ii = (i + k) % k2
		if j < k:
			v1 = cacc[j + k - 1][i] - (0 if j == 0 else cacc[j - 1][i])
			v2 = cacc[k2 - 1][ii] - (cacc[j + k - 1][ii] - (0 if j == 0 else cacc[j - 1][ii]))
		else:
			jj = j - k
			v1 = cacc[k2 - 1][i] - (cacc[jj + k - 1][i] - (0 if jj == 0 else cacc[jj - 1][i]))
			v2 = cacc[jj + k - 1][ii] - (0 if jj == 0 else cacc[jj - 1][ii])
		mx = max(mx, v1 + v2)
print(mx)


def main():
	import sys
	input = sys.stdin.readline
	(N, K) = map(int, input().split())
	BMAP = [[0] * (2 * K) for j in range(2 * K)]
	WMAP = [[0] * (2 * K) for j in range(2 * K)]
	for i in range(N):
		(x, y, c) = input().split()
		x = int(x) % (2 * K)
		y = int(y) % (2 * K)
		if c == 'B':
			BMAP[y][x] += 1
		else:
			WMAP[y][x] += 1
	import copy
	Bruiseki = copy.deepcopy(BMAP)
	Wruiseki = copy.deepcopy(WMAP)
	for i in range(2 * K):
		for j in range(1, 2 * K):
			Bruiseki[i][j] += Bruiseki[i][j - 1]
			Wruiseki[i][j] += Wruiseki[i][j - 1]
	for i in range(2 * K):
		for j in range(1, 2 * K):
			Bruiseki[j][i] += Bruiseki[j - 1][i]
			Wruiseki[j][i] += Wruiseki[j - 1][i]
	ans = 0
	for i in range(K):
		for j in range(K):
			Bone = Bruiseki[i][j]
			Btwo = Bruiseki[i][j + K] - Bone
			Bthree = Bruiseki[i][2 * K - 1] - Bruiseki[i][j + K]
			Bfour = Bruiseki[i + K][j] - Bone
			Bfive = Bruiseki[i + K][j + K] - Bone - Btwo - Bfour
			Bsix = Bruiseki[i + K][2 * K - 1] - Bruiseki[i + K][j + K] - Bthree
			Bseven = Bruiseki[2 * K - 1][j] - Bruiseki[i + K][j]
			Beight = Bruiseki[2 * K - 1][j + K] - Bruiseki[i + K][j + K] - Bseven
			Bnine = Bruiseki[2 * K - 1][2 * K - 1] - Bruiseki[2 * K - 1][j + K] - Bthree - Bsix
			Wone = Wruiseki[i][j]
			Wtwo = Wruiseki[i][j + K] - Wone
			Wthree = Wruiseki[i][2 * K - 1] - Wruiseki[i][j + K]
			Wfour = Wruiseki[i + K][j] - Wone
			Wfive = Wruiseki[i + K][j + K] - Wone - Wtwo - Wfour
			Wsix = Wruiseki[i + K][2 * K - 1] - Wruiseki[i + K][j + K] - Wthree
			Wseven = Wruiseki[2 * K - 1][j] - Wruiseki[i + K][j]
			Weight = Wruiseki[2 * K - 1][j + K] - Wruiseki[i + K][j + K] - Wseven
			Wnine = Wruiseki[2 * K - 1][2 * K - 1] - Wruiseki[2 * K - 1][j + K] - Wthree - Wsix
			tmp1 = Wone + Wthree + Wfive + Wseven + Wnine + Btwo + Bfour + Bsix + Beight
			tmp2 = Bone + Bthree + Bfive + Bseven + Bnine + Wtwo + Wfour + Wsix + Weight
			if tmp1 > ans:
				ans = tmp1
			if tmp2 > ans:
				ans = tmp2
	print(ans)
main()


import sys
import numpy as np
(n, k) = list(map(int, sys.stdin.readline().strip().split(' ')))
xyc = [l.strip().split(' ') for l in sys.stdin.readlines()]
xy = [(int(_x) % (2 * k), int(_y) % (2 * k) if _c == 'W' else (int(_y) + k) % (2 * k)) for (_x, _y, _c) in xyc]
ans = [[0 for j in range(2 * k + 1)] for i in range(2 * k + 1)]
for (_x, _y) in xy:
	if (_x - k + 0.5) * (_y - k + 0.5) > 0:
		_x %= k
		_y %= k
		ans[_x][_y] += 2
		ans[_x + k][_y] -= 2
		ans[_x][_y + k] -= 2
		ans[_x + k][_y + k] += 2
		ans[_x + k][0] += 1
		ans[0][_y + k] += 1
		ans[0][0] += 1
		ans[_x][0] -= 1
		ans[0][_y] -= 1
	else:
		_x %= k
		_y %= k
		ans[_x][_y] -= 2
		ans[_x + k][_y] += 2
		ans[_x][_y + k] += 2
		ans[_x + k][_y + k] -= 2
		ans[_x + k][0] -= 1
		ans[0][_y + k] -= 1
		ans[_x][0] += 1
		ans[0][_y] += 1
print(np.asarray(ans).cumsum(axis=1).cumsum(axis=0).max())


def main():
	na = list(map(int, input().split()))
	N = na[0]
	K = na[1]
	x = []
	y = []
	c = []
	for i in range(N):
		sa = input().split()
		x.append(int(sa[0]))
		y.append(int(sa[1]))
		c.append(True if sa[2] == 'W' else False)
	hopes = [[0 for j in range(K)] for i in range(K)]
	ysum = [[0 for j in range(K * 2)] for i in range(K)]
	score = 0
	for i in range(N):
		xmod = x[i] % K
		ymod = y[i] % K
		f = (int(x[i] / K) + int(y[i] / K)) % 2 == 1
		c[i] ^= f
		if c[i]:
			score += 1
		h = -1 if c[i] else 1
		hopes[xmod][ymod] += h
	for i in range(K):
		s = 0
		for j in range(K):
			s += hopes[i][j]
			ysum[i][j] = s
		for j in range(K):
			s -= hopes[i][j]
			ysum[i][K + j] = s
	smax = 0
	for j in range(K):
		s = 0
		for i in range(K):
			s += ysum[i][K + j]
		for i in range(K):
			s += ysum[i][j] - ysum[i][K + j]
			smax = max(smax, s)
		for i in range(K):
			s -= ysum[i][j] - ysum[i][K + j]
			smax = max(smax, s)
	print(score + smax)
main()


(n, k) = map(int, input().split())
square = [[0 for i in range(k * 2)] for j in range(k * 2)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'W':
		y += k
	x = x % (k * 2)
	y = y % (k * 2)
	square[x][y] += 1

class CumulativeSum2d:

	def __init__(self, collection_2d):
		yl = len(collection_2d)
		xl = len(collection_2d[0])
		self.data = [[0] * (xl + 1) for _ in range(yl + 1)]
		for y in range(1, yl + 1):
			for x in range(1, xl + 1):
				top_sum = self.data[y - 1][x]
				left_sum = self.data[y][x - 1]
				dup_sum = self.data[y - 1][x - 1]
				self.data[y][x] = top_sum + left_sum - dup_sum
				self.data[y][x] += collection_2d[y - 1][x - 1]

	def query(self, x, y, x1, y1):
		return self.data[y1][x1] - self.data[y - 1][x1] - self.data[y1][x - 1] + self.data[y - 1][x - 1]
cs = CumulativeSum2d(square)
ans = 0
for x in range(1, k * 2 + 1):
	for y in range(1, k * 2 + 1):
		sumd = 0
		sumd += cs.query(x, y, min(k * 2, x + k - 1), min(k * 2, y + k - 1))
		if x - k > 1:
			sumd += cs.query(1, y, x - k - 1, min(k * 2, y + k - 1))
		if y - k > 1:
			sumd += cs.query(x, 1, min(k * 2, x + k - 1), y - k - 1)
		if x > 1 and y > 1:
			sumd += cs.query(max(1, x - k), max(1, y - k), x - 1, y - 1)
		if x > 1 and y - k < 1:
			sumd += cs.query(max(1, x - k), y + k, x - 1, k * 2)
		if x - k < 1 and y > 1:
			sumd += cs.query(x + k, max(1, y - k), k * 2, y - 1)
		if x - k < 1 and y - k < 1:
			sumd += cs.query(x + k, y + k, k * 2, k * 2)
		ans = max(sumd, ans)
print(ans, flush=True)


import numpy as np
(N, K) = map(int, input().split())
K2 = 2 * K
Mij = [[0 for _ in range(K2 + 1)] for _ in range(K2 + 1)]

def imos(x1, y1, x2, y2):
	Mij[y1][x1] += 1
	Mij[y2][x2] += 1
	Mij[y2][x1] -= 1
	Mij[y1][x2] -= 1
for _ in range(N):
	ls = input().split()
	x = int(ls[0]) % K2
	y = int(ls[1]) % K2
	if ls[2] == 'B':
		y = (y + K) % K2
	x_ = x % K
	y_ = y % K
	if x < K and y < K or (K <= x and K <= y):
		imos(0, 0, x_ + 1, y_ + 1)
		imos(x_ + K + 1, 0, K2, y_ + 1)
		imos(0, y_ + K + 1, x_ + 1, K2)
		imos(x_ + K + 1, y_ + K + 1, K2, K2)
		imos(x_ + 1, y_ + 1, x_ + K + 1, y_ + K + 1)
	else:
		imos(x_ + 1, 0, x_ + K + 1, y_ + 1)
		imos(x_ + 1, y_ + K + 1, x_ + K + 1, K2)
		imos(0, y_ + 1, x_ + 1, y_ + K + 1)
		imos(x_ + K + 1, y_ + 1, K2, y_ + K + 1)
Mij = np.array(Mij)
for x in range(K2):
	Mij[:, x + 1] += Mij[:, x]
for y in range(K2):
	Mij[y + 1, :] += Mij[y, :]
print(Mij.max())


import gc
(n, k) = map(int, input().split())
k2 = k * 2
b = [[0] * (k * 3) for _ in range(k * 3)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x) % k2
	y = int(y) % k2
	if c == 'W':
		x = (x + k) % k2
	b[x][y] += 1
	if x < k:
		b[x + k2][y] += 1
	if y < k:
		b[x][y + k2] += 1
	if x < k and y < k:
		b[x + k2][y + k2] += 1
b_cs = [[0] * (k * 3 + 1) for _ in range(k * 3 + 1)]
for i in range(k * 3):
	for j in range(k * 3):
		b_cs[i + 1][j + 1] = b_cs[i + 1][j] + b_cs[i][j + 1] - b_cs[i][j] + b[i][j]

def func_b(x, y):
	return b_cs[x + k][y + k] - b_cs[x + k][y] - b_cs[x][y + k] + b_cs[x][y]
ans = 0
for i in range(k):
	for j in range(k):
		cand = 0
		cand += func_b(i, j)
		cand += func_b(i + k, j + k)
		ans = max(ans, cand)
		cand = 0
		cand += func_b(i + k, j)
		cand += func_b(i, j + k)
		ans = max(ans, cand)
print(ans)


def d_checker(N, K, Q):
	mat = [[0] * (2 * K + 2) for _ in range(2 * K + 2)]
	total = [[0] * (4 * K + 2) for _ in range(4 * K + 2)]
	for (x, y, c) in Q:
		if c == 'W':
			y += K
		mat[x % (2 * K) + 1][y % (2 * K) + 1] += 1
	for x in range(1, 4 * K + 1):
		for y in range(1, 4 * K + 1):
			total[x][y] = total[x][y - 1] + mat[(x - 1) % (2 * K) + 1][(y - 1) % (2 * K) + 1]
	for y in range(1, 4 * K + 1):
		for x in range(1, 4 * K + 1):
			total[x][y] += total[x - 1][y]

	def get(x, y):
		return total[x + K - 1][y + K - 1] + total[x - 1][y - 1] - total[x - 1][y + K - 1] - total[x + K - 1][y - 1]
	ans = 0
	for x in range(1, 2 * K + 1):
		for y in range(1, 2 * K + 1):
			ans = max(ans, get(x, y) + get(x + K, y + K))
	return ans
(N, K) = [int(i) for i in input().split()]
Q = [(int(x), int(y), c) for (x, y, c) in [input().split() for i in range(N)]]
print(d_checker(N, K, Q))


import sys
import numpy as np

def main():
	input = sys.stdin.readline

	def gen_pattern(x):
		if x < K - 1:
			return [[0, x + 1], [x + K + 1, 2 * K]]
		else:
			return [[x - K + 1, x + 1]]

	def inputs():
		return [int(x) for x in input().split()]

	def inputstr():
		return list(input().split())
	(N, K) = inputs()
	BB = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
	for i in range(N):
		(x, y, c) = inputstr()
		if c == 'W':
			y = int(y) + K
		x = int(x) % (2 * K)
		y = int(y) % (2 * K)
		for shift in [0, K]:
			for (b, t) in gen_pattern((y + shift) % (2 * K)):
				for (l, r) in gen_pattern((x + shift) % (2 * K)):
					BB[b][l] += 1
					BB[b][r] -= 1
					BB[t][l] -= 1
					BB[t][r] += 1
	ans = np.max(np.cumsum(np.cumsum(BB, axis=1), axis=0))
	print(int(ans))
main()


import numpy as np
(N, K) = list(map(int, input().split()))
KK = 2 * K
hope = np.zeros((KK, KK), dtype=np.int32)
XYC = [input().split() for _ in range(N)]
for (x, y, c) in XYC:
	x = int(x)
	y = int(y)
	if c == 'B':
		x -= K
	x %= KK
	y %= KK
	hope[x, y] += 1
A = np.zeros((KK + K, KK + K), dtype=np.int32)
A[:-K, :-K] = hope[:]
A[K:, :-K] += -hope[:]
A[:-K, K:] += -hope[:]
A[K:, K:] += hope[:]
A = A.cumsum(axis=0).cumsum(axis=1)
B = A[:, :KK]
B[:, :K] += A[:, KK:]
A = B[:KK, :]
A[:K, :] += B[KK:, :]
B = A[:K, :]
B[:, :K] += A[K:, K:]
B[:, K:] += A[K:, :K]
answer = B.max()
print(answer)


class Accumulate2D:

	def __init__(self, a):
		self.height = len(a)
		self.width = len(a[0])
		self.acc = self.accumulate(a)

	def accumulate(self, a):
		b = [[0] * (self.width + 1) for i in range(self.height + 1)]
		b[1][1] = a[0][0]
		for i in range(self.height):
			for j in range(self.width):
				b[i + 1][j + 1] = b[i + 1][j] + b[i][j + 1] - b[i][j] + a[i][j]
		return b

	def sum(self, p, q):
		(y1, x1) = p
		(y2, x2) = q
		b = self.acc
		if y1 > y2 or x1 > x2:
			return 0
		else:
			return b[y2 + 1][x2 + 1] - b[y2 + 1][x1] - b[y1][x2 + 1] + b[y1][x1]
(n, k) = map(int, input().split())
kk = k * 2
z = [[0] * kk for i in range(kk)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x) % kk
	y = int(y) % kk
	if c == 'W':
		x = (x + k) % kk
	z[x][y] += 1
z = Accumulate2D(z)
ans = 0
for i in range(k):
	for j in range(k):
		a = z.sum([0, 0], [i - 1, j - 1]) + z.sum([i, j], [i + k - 1, j + k - 1]) + z.sum([0, j + k], [i - 1, kk - 1]) + z.sum([i + k, 0], [kk - 1, j - 1]) + z.sum([i + k, j + k], [kk - 1, kk - 1])
		ans = max(ans, a, n - a)
print(ans)


import numpy as np

def imos2d(x1, y1, x2, y2, board):
	board[y1][x1] += 1
	board[y2][x2] += 1
	board[y2][x1] -= 1
	board[y1][x2] -= 1

def __starting_point():
	(N, K) = list(map(int, input().split()))
	lines = [input() for _ in range(N)]
	K2 = 2 * K
	add_dict = {'W': 0, 'B': K}
	board = [[0 for _ in range(K2 + 1)] for _ in range(K2 + 1)]
	for line in lines:
		ls = line.split()
		x = int(ls[0]) % K2
		y = (int(ls[1]) + add_dict[ls[2]]) % K2
		x_ = x % K
		y_ = y % K
		if x < K and y < K or (K <= x and K <= y):
			imos2d(0, 0, x_ + 1, y_ + 1, board)
			imos2d(x_ + K + 1, 0, K2, y_ + 1, board)
			imos2d(0, y_ + K + 1, x_ + 1, K2, board)
			imos2d(x_ + K + 1, y_ + K + 1, K2, K2, board)
			imos2d(x_ + 1, y_ + 1, x_ + K + 1, y_ + K + 1, board)
		else:
			imos2d(x_ + 1, 0, x_ + K + 1, y_ + 1, board)
			imos2d(x_ + 1, y_ + K + 1, x_ + K + 1, K2, board)
			imos2d(0, y_ + 1, x_ + 1, y_ + K + 1, board)
			imos2d(x_ + K + 1, y_ + 1, K2, y_ + K + 1, board)
	board = np.array(board)
	for x in range(K2):
		board[:, x + 1] += board[:, x]
	for y in range(K2):
		board[y + 1, :] += board[y, :]
	print(board.max())
__starting_point()


import sys
from itertools import accumulate
input = sys.stdin.readline

def main():
	(N, K) = map(int, input().split())
	TK = K * 2
	B = [[0] * (K * 2) for _ in range(TK)]
	for _ in range(N):
		(x, y, c) = input().split()
		(x, y) = (int(x), int(y))
		if c == 'B':
			B[x % TK][y % TK] += 1
		else:
			B[x % TK][(y + K) % TK] += 1
	C = [[0] * (TK + 1)] + [[0] + list(accumulate(B[i])) for i in range(TK)]
	for i in range(TK):
		for j in range(TK + 1):
			C[i + 1][j] += C[i][j]

	def get(i1, j1, i2, j2):
		return C[i2][j2] - C[i1][j2] - C[i2][j1] + C[i1][j1]
	ans = 0
	for i in range(K + 1):
		for j in range(K + 1):
			if ans < get(0, 0, i, j) + get(i + K, 0, TK, j) + get(i, j, i + K, j + K) + get(0, j + K, i, TK) + get(K + i, K + j, TK, TK):
				ans = get(0, 0, i, j) + get(i + K, 0, TK, j) + get(i, j, i + K, j + K) + get(0, j + K, i, TK) + get(K + i, K + j, TK, TK)
			if ans < get(i, 0, i + K, j) + get(0, j, i, j + K) + get(i + K, j, TK, j + K) + get(i, j + K, i + K, TK):
				ans = get(i, 0, i + K, j) + get(0, j, i, j + K) + get(i + K, j, TK, j + K) + get(i, j + K, i + K, TK)
	print(ans)
main()


from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(2147483647)
INF = 10 ** 13

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def I():
	return int(sys.stdin.buffer.readline())

def LS():
	return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S():
	return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]

def SRL(n):
	return [list(S()) for i in range(n)]

def MSRL(n):
	return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
(n, k) = LI()
acc = [[0] * 3 * k for _ in range(2 * k)]
for _ in range(n):
	(x, y, c) = LS()
	x = int(x)
	y = int(y)
	if c == 'W':
		x += k
	x %= 2 * k
	y %= 2 * k
	acc[y][x] += 1
	for (i, j) in ((1, 1), (1, -1), (-1, 1), (-1, -1), (0, 2)):
		if 0 <= y + k * i < 2 * k and 0 <= x + k * j < 3 * k:
			acc[y + k * i][x + k * j] += 1
acc = [[0] * (3 * k + 1)] + [list(accumulate([0] + acc[y])) for y in range(2 * k)]
for y in range(2 * k):
	for x in range(3 * k + 1):
		acc[y + 1][x] += acc[y][x]
ans = 0
for y in range(k + 1):
	for x in range(2 * k + 1):
		ans = max(ans, acc[y + k][x + k] - acc[y][x + k] - acc[y + k][x] + acc[y][x])
print(ans)


(n, k) = map(int, input().split())
s = [[0] * k for _ in range(2 * k + 1)]
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	x -= x // (2 * k) * (2 * k)
	y -= y // (2 * k) * (2 * k)
	if x >= k and y >= k:
		x -= k
		y -= k
	elif y >= k:
		x += k
		y -= k
	if c == 'B':
		if x >= k:
			x -= k
		else:
			x += k
	s[x + 1][y] += 1
for i in range(2 * k):
	for j in range(k):
		s[i + 1][j] += s[i][j]
ans = 0
for i in range(k):
	cnt = 0
	for j in range(k):
		cnt += s[i + k][j] - s[i][j]
	ans = max(ans, cnt, n - cnt)
	for j in range(k):
		cnt -= 2 * (s[i + k][j] - s[i][j])
		cnt += s[2 * k][j]
		ans = max(ans, cnt, n - cnt)
print(ans)


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
ps = []
table = [[0] * (2 * K) for _ in range(2 * K)]
res = 0
ws = 0
for i in range(N):
	(u, v, t) = input().split()
	u = int(u)
	v = int(v)
	ps.append((u % (2 * K), v % (2 * K), t))
	table[u % (2 * K)][v % (2 * K)] += (-1) ** int(t == 'W')
	table[(u + K) % (2 * K)][(v + K) % (2 * K)] += (-1) ** int(t == 'W')
	ws += t == 'W'
cs = [[0] * (2 * K + 1) for _ in range(2 * K)]
for i in range(2 * K):
	for j in range(2 * K):
		cs[i][j + 1] = cs[i][j] + table[i][j]
for i in range(2 * K):
	t = 0
	tres = 0
	for j in range(K):
		t += cs[j][min(2 * K, i + K)] + cs[j][max(0, i - K)] - cs[j][i]
	tres = max(tres, t + ws)
	for j in range(2 * K):
		t -= cs[j][min(2 * K, i + K)] + cs[j][max(0, i - K)] - cs[j][i]
		t += cs[(j + K) % (2 * K)][min(2 * K, i + K)] + cs[(j + K) % (2 * K)][max(0, i - K)] - cs[(j + K) % (2 * K)][i]
		tres = max(tres, t + ws)
	res = max(res, tres)
print(res)


def da_generate(h, w, a):
	da = [[0] * w for j in range(h)]
	da[0][0] = a[0][0]
	for i in range(1, w):
		da[0][i] = da[0][i - 1] + a[0][i]
	for i in range(1, h):
		cnt_w = 0
		for j in range(w):
			cnt_w += a[i][j]
			da[i][j] = da[i - 1][j] + cnt_w
	return da

def da_calc(da, p, q, x, y):
	if p > x or q > y:
		return 0
	if p == 0 and q == 0:
		return da[x][y]
	if p == 0:
		return da[x][y] - da[x][q - 1]
	if q == 0:
		return da[x][y] - da[p - 1][y]
	return da[x][y] - da[p - 1][y] - da[x][q - 1] + da[p - 1][q - 1]

def examD():
	(N, K) = LI()
	colorXY = [[0] * (2 * K) for _ in range(2 * K)]
	for _ in range(N):
		(x, y, c) = LSI()
		(x, y) = (int(x), int(y))
		if c == 'B':
			y += K
		colorXY[x % (2 * K)][y % (2 * K)] += 1
	ans = 0
	ansC = N
	da = da_generate(2 * K, 2 * K, colorXY)
	for i in range(K):
		for j in range(K):
			cur = da_calc(da, 0, 0, K - i - 1, K - j - 1) + da_calc(da, K - i, K - j, 2 * K - i - 1, 2 * K - j - 1) + da_calc(da, 0, 2 * K - j, K - i - 1, 2 * K - 1) + da_calc(da, 2 * K - i, 0, 2 * K - 1, K - j - 1) + da_calc(da, 2 * K - i, 2 * K - j, 2 * K - 1, 2 * K - 1)
			ans = max(ans, cur)
			ansC = min(ansC, cur)
	print(max(ans, N - ansC))
import sys, copy, bisect, itertools, heapq, math
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LSI():
	return list(map(str, sys.stdin.readline().split()))

def LS():
	return sys.stdin.readline().split()

def S():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
inf = float('inf')
examD()


(n, k) = map(int, input().split())
ans = [[0] * (3 * k + 10) for i in range(4 * k + 10)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	a = x // k % 2
	b = x % k
	d = y // k % 2
	e = y % k
	x = b + a * k
	y = e + d * k
	if (a + d) % 2 == 0:
		if c == 'B':
			x = k - b
			y = k - e
		else:
			x = 2 * k - b
			y = k - e
	elif c == 'W':
		x = k - b
		y = k - e
	else:
		x = 2 * k - b
		y = k - e
	ans[x][y] += 1
	ans[x][y + k] -= 1
	ans[x + k][y] -= 1
	ans[x + k][y + k] += 1
for i in range(4 * k + 9):
	for j in range(3 * k + 9):
		ans[i][j + 1] = ans[i][j] + ans[i][j + 1]
for i in range(4 * k + 9):
	for j in range(3 * k + 9):
		ans[i + 1][j] = ans[i][j] + ans[i + 1][j]
anser = 0
for x in range(k, 2 * k):
	for y in range(k, 2 * k):
		p = ans[x][y] + ans[x - k][y - k] + ans[x - k][y + k] + ans[x + k][y + k] + ans[x + k][y - k] + ans[x + 2 * k][y]
		anser = max(anser, p)
for x in range(2 * k, 3 * k):
	for y in range(k, 2 * k):
		p = ans[x][y] + ans[x - k][y - k] + ans[x - k][y + k] + ans[x + k][y + k] + ans[x + k][y - k] + ans[x - 2 * k][y]
		anser = max(anser, p)
print(anser)


import sys
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().rstrip()
YesNo = lambda b: bool([print('Yes')] if b else print('No'))
YESNO = lambda b: bool([print('YES')] if b else print('NO'))
int1 = lambda x: int(x) - 1

def main():
	(N, K) = map(int, input().split())
	a = [[[0] * K * 2 for _ in range(K * 2)] for _ in range(2)]
	for _ in range(N):
		(x, y, c) = input().split()
		(x, y) = (int(x) % (K * 2), int(y) % (K * 2))
		if c == 'W':
			a[0][y][x] += 1
		else:
			a[1][y][x] += 1
	S = [[[0] * (K * 2 + 1) for _ in range(K * 2 + 1)] for _ in range(2)]
	for k in range(2):
		for i in range(K * 2):
			for j in range(K * 2):
				S[k][i + 1][j + 1] = S[k][i][j + 1] + S[k][i + 1][j] - S[k][i][j] + a[k][i][j]

	def getsum(k, y1, y2, x1, x2):
		return S[k][x2][y2] - S[k][x1][y2] - S[k][x2][y1] + S[k][x1][y1]
	ans = 0
	for k in range(2):
		for i in range(K + 1):
			for j in range(K + 1):
				tmp = 0
				tmp += getsum(k, 0, i, 0, j) + getsum(k, i + K, K * 2, 0, j) + getsum(k, 0, i, j + K, K * 2) + getsum(k, i + K, K * 2, j + K, K * 2) + getsum(k, i, i + K, j, j + K)
				tmp += getsum(not k, i, i + K, 0, j) + getsum(not k, i, i + K, j + K, K * 2) + getsum(not k, 0, i, j, j + K) + getsum(not k, i + K, K * 2, j, j + K)
				ans = max(ans, tmp)
	print(ans)
main()


import numpy as np

def solve(string):
	(n, k, *xyc) = string.split()
	k = int(k)
	l = 2 * int(k)
	xy = [(int(x) % l, int(y) % l) if c == 'W' else (int(x) % l, (int(y) + k) % l) for (x, y, c) in zip(xyc[::3], xyc[1::3], xyc[2::3])]
	ans = [[0 for j in range(l + 0)] for i in range(l + 0)]
	for (_x, _y) in xy:
		if (_x - k + 0.5) * (_y - k + 0.5) > 0:
			lt = 2
			lte = 1
			ans[0][0] += 1
		else:
			lt = -2
			lte = -1
		_x %= k
		_y %= k
		ans[_x][_y] += lt
		ans[_x + k][_y] -= lt
		ans[_x][_y + k] -= lt
		ans[_x + k][_y + k] += lt
		ans[_x + k][0] += lte
		ans[0][_y + k] += lte
		ans[_x][0] -= lte
		ans[0][_y] -= lte
	return str(np.asarray(ans).cumsum(axis=1).cumsum(axis=0).max())

def __starting_point():
	(n, m) = list(map(int, input().split()))
	print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))
__starting_point()


(N, K) = map(int, input().split())
X = [[0] * (2 * K) for _ in range(3 * K)]
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'B':
		x += K
	x %= 2 * K
	y %= 2 * K
	if y >= K:
		(x, y) = ((x + K) % (2 * K), y - K)
	for (dx, dy) in ((0, 0), (2 * K, 0), (K, K), (-K, K)):
		(xx, yy) = (x + dx, y + dy)
		if 0 <= xx < 3 * K and 0 <= yy < 2 * K:
			X[xx][yy] += 1
for i in range(1, 3 * K):
	for j in range(2 * K):
		X[i][j] += X[i - 1][j]
for i in range(3 * K):
	for j in range(1, 2 * K):
		X[i][j] += X[i][j - 1]
ma = 0
for i in range(2 * K):
	for j in range(K):
		ma = max(ma, X[i + K][j + K] - X[i][j + K] - X[i + K][j] + X[i][j])
print(ma)


import sys
(N, K) = map(int, input().split())
(x, y, c) = ([0] * N, [0] * N, [0] * N)
board = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
MOD = 2 * K + 1
for i in range(N):
	(x[i], y[i], c[i]) = input().split()
	(x[i], y[i]) = (int(x[i]), int(y[i]))
	if c[i] == 'W':
		x[i] += K
		c[i] = 'B'
	x[i] %= 2 * K
	y[i] %= 2 * K
	board[y[i] + 1][x[i] + 1] += 1
for i in range(2 * K + 1):
	for j in range(2 * K):
		board[i][j + 1] += board[i][j]
for j in range(2 * K + 1):
	for i in range(2 * K):
		board[i + 1][j] += board[i][j]
ans = 0

def c(x1, y1):
	global K, board
	x1 = x1 % MOD + x1 // MOD
	y1 = y1 % MOD + y1 // MOD
	x2 = (x1 + K) % MOD + (x1 + K) // MOD
	y2 = (y1 + K) % MOD + (y1 + K) // MOD
	if x1 < x2:
		(x11, x12, x21, x22) = (x1, x2, 0, 0)
	else:
		(x11, x12, x21, x22) = (x1, -1, 0, x2)
	if y1 < y2:
		(y11, y12, y21, y22) = (y1, y2, 0, 0)
	else:
		(y11, y12, y21, y22) = (y1, -1, 0, y2)
	f = lambda a, b, c, d: board[a][b] + board[c][d] - board[a][d] - board[c][b]
	ret = 0
	ret += f(y12, x12, y11, x11)
	ret += f(y22, x22, y21, x21)
	ret += f(y12, x22, y11, x21)
	ret += f(y22, x12, y21, x11)
	return ret
for i in range(2 * K + 1):
	for j in range(K + 1):
		ans = max((ans, c(i, j) + c(i + K, j + K)))
print(ans)


import numpy as np
(N, K) = map(int, input().split())
p = [list(input().split()) for _ in range(N)]
grid = [[[0] * 2 for j in range(K)] for i in range(K)]
for i in range(N):
	(x, y, c) = p[i]
	(x, y) = (int(x), int(y))
	(mx, my) = (x % K, y % K)
	f = False
	if x // K % 2 != y // K % 2:
		f = not f
	if c == 'W':
		f = not f
	if f:
		grid[0][0][0] += 1
		grid[mx][0][0] -= 1
		grid[0][my][0] -= 1
		grid[mx][my][0] += 2
	else:
		grid[0][my][0] += 1
		grid[mx][0][0] += 1
		grid[mx][my][0] -= 2
	f = False
	if x // K % 2 != y // K % 2:
		f = not f
	if c == 'B':
		f = not f
	if f:
		grid[0][0][1] += 1
		grid[mx][0][1] -= 1
		grid[0][my][1] -= 1
		grid[mx][my][1] += 2
	else:
		grid[0][my][1] += 1
		grid[mx][0][1] += 1
		grid[mx][my][1] -= 2
grid = np.array(grid)
grid = np.cumsum(grid, axis=0)
grid = np.cumsum(grid, axis=1)
print(int(np.max(grid)))


import sys
input = sys.stdin.readline
import math
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
MOD = 10 ** 9 + 7
INF = float('inf')
AZ = 'abcdefghijklmnopqrstuvwxyz'

def I():
	return int(input().strip())

def S():
	return input().strip()

def IL():
	return list(map(int, input().split()))

def SL():
	return list(map(str, input().split()))

def ILs(n):
	return list((int(input()) for _ in range(n)))

def SLs(n):
	return list((input().strip() for _ in range(n)))

def ILL(n):
	return [list(map(int, input().split())) for _ in range(n)]

def SLL(n):
	return [list(map(str, input().split())) for _ in range(n)]
(N, K) = IL()
data = SLL(N)
base = 10 ** 6
BW_data = [[0 for i in range(2 * K)] for i in range(2 * K)]
for (x, y, c) in data:
	(x, y) = (int(x) % (2 * K), int(y) % (2 * K))
	if c == 'B':
		BW_data[y][x] += 1
	else:
		BW_data[y][x] += base

def ACC2(l):
	H = len(l)
	W = len(l[0])
	ret = [[0] * (W + 1) for i in range(H + 1)]
	for i in range(H):
		for j in range(W):
			ret[i + 1][j + 1] = ret[i + 1][j] + ret[i][j + 1] - ret[i][j] + l[i][j]
	return ret

def RANGE_SUM_in_ACC(l, h1, h2, w1, w2):
	return l[h2][w2] - l[h2][w1] - l[h1][w2] + l[h1][w1]
BW_ACC = ACC2(BW_data)
BW_res = [[0 for i in range(2 * K)] for i in range(K)]
for j in range(K):
	for i in range(2 * K):
		if i < K:
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, j, j + K, i, i + K)
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, 0, max(0, j), 0, max(0, i))
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, 0, max(0, j), min(2 * K, i + K), 2 * K)
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, min(2 * K, j + K), 2 * K, 0, max(0, i))
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, min(2 * K, j + K), 2 * K, min(2 * K, i + K), 2 * K)
		else:
			BW_res[j][i] = BW_ACC[-1][-1] - BW_res[j][i - K]
ans = 0
for j in range(K):
	for i in range(2 * K):
		ans = max(ans, BW_res[j][i] % base + BW_res[j][(i + K) % (2 * K)] // base)
print(ans)


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
L = [[0 for i in range(K * 2 + 1)] for j in range(K * 2 + 1)]
for i in range(N):
	(x, y, c) = list(input().split())
	if c == 'W':
		x = int(x) + K
		y = int(y)
	else:
		x = int(x)
		y = int(y)
	L[x % (2 * K) + 1][y % (2 * K) + 1] += 1
for h in range(1, 2 * K + 1):
	for w in range(1, 2 * K + 1):
		L[h][w] = L[h - 1][w] + L[h][w]
for h in range(1, 2 * K + 1):
	for w in range(1, 2 * K + 1):
		L[h][w] = L[h][w - 1] + L[h][w]
ans = 0
for h in range(1, K + 1):
	for w in range(1, K + 1):
		cnt = 0
		for a in range(3):
			for b in range(3):
				if (a + b) % 2 == 0:
					HH = h + a * K
					if h + a * K > 2 * K:
						HH = 2 * K
					HL = h + a * K - K
					if h + a * K - K < 0:
						HL = 0
					WH = w + b * K
					if w + b * K > 2 * K:
						WH = 2 * K
					WL = w + b * K - K
					if w + b * K - K < 0:
						WL = 0
					cnt += L[HH][WH] + L[HL][WL] - L[HH][WL] - L[HL][WH]
		ans = max(ans, max(cnt, N - cnt))
print(ans)


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
(N, K) = MAP()
mod = K * 2
XY = []
MAX = K * 4
acc = list2d(MAX + 1, MAX + 1, 0)
for i in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	x %= mod
	y %= mod
	if c == 'B':
		x += K
	for a1 in range(3):
		for a2 in range(3):
			for b in [0, K]:
				if c == 'B':
					b = -b
				x1 = x + a1 * mod + b
				y1 = y + a2 * mod + b
				x2 = x1 + K
				y2 = y1 + K
				x1 = max(min(x1, MAX), 0)
				y1 = max(min(y1, MAX), 0)
				x2 = max(min(x2, MAX), 0)
				y2 = max(min(y2, MAX), 0)
				acc[x1][y1] += 1
				acc[x2][y2] += 1
				acc[x1][y2] -= 1
				acc[x2][y1] -= 1
for i in range(MAX + 1):
	acc[i] = list(accumulate(acc[i]))
for j in range(MAX + 1):
	for i in range(MAX):
		acc[i + 1][j] += acc[i][j]
ans = 0
for i in range(K * 2, K * 4 + 1):
	ans = max(ans, max(acc[i][K * 2:K * 4 + 1]))
print(ans)


(N, K) = map(int, input().split())
want_white = [[0] * (K + 1) for _ in range(K + 1)]
want_black = [[0] * (K + 1) for _ in range(K + 1)]
for _ in range(N):
	(x_str, y_str, c_str) = input().split()
	x = int(x_str)
	y = int(y_str)
	c = 1 if c_str == 'B' else 0
	x %= 2 * K
	y %= 2 * K
	if x >= K:
		x -= K
		c ^= 1
	if y >= K:
		y -= K
		c ^= 1
	if c:
		want_black[x + 1][y + 1] += 1
	else:
		want_white[x + 1][y + 1] += 1
for i in range(K + 1):
	for j in range(K):
		want_black[i][j + 1] += want_black[i][j]
		want_white[i][j + 1] += want_white[i][j]
for i in range(K):
	for j in range(K + 1):
		want_black[i + 1][j] += want_black[i][j]
		want_white[i + 1][j] += want_white[i][j]
ans = 0
for h in range(K + 1):
	for w in range(K + 1):
		count = want_black[-1][-1] - want_black[h][-1] - want_black[-1][w] + 2 * want_black[h][w]
		count += want_white[h][-1] + want_white[-1][w] - 2 * want_white[h][w]
		ans = max(ans, count, N - count)
print(ans)


import sys

def input():
	return sys.stdin.readline().strip()
(N, K) = map(int, input().split())
B = [[0] * (K * 4) for i in range(K * 4)]
for i in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += K
	B[x % (K * 2)][y % (K * 2)] += 1
for i in range(K * 4):
	for j in range(K * 4):
		B[i][j] = B[i % (K * 2)][j % (K * 2)]
for i in range(K * 4):
	for j in range(K * 4):
		if j > 0:
			B[i][j] += B[i][j - 1]
for i in range(K * 4):
	for j in range(K * 4):
		if i > 0:
			B[i][j] += B[i - 1][j]

def get_sum(x1, y1, x2, y2, sm):
	res = sm[x2][y2]
	if x1 > 0:
		res -= sm[x1 - 1][y2]
	if y1 > 0:
		res -= sm[x2][y1 - 1]
	if x1 > 0 and y1 > 0:
		res += sm[x1 - 1][y1 - 1]
	return res
ans = 0
for i in range(K * 2):
	for j in range(K * 2):
		tmp = get_sum(i, j, i + K - 1, j + K - 1, B)
		tmp += get_sum(i + K, j + K, i + K * 2 - 1, j + K * 2 - 1, B)
		ans = max(tmp, ans)
print(ans)


class Acc2D:

	def __init__(self, a, K):
		(h, w) = (len(a), len(a[0]))
		self.acc2D = self._build(h, w, a)
		self.K = K

	def _build(self, h, w, a):
		ret = [[0] * (w + 1) for _ in range(h + 1)]
		for r in range(h):
			for c in range(w):
				ret[r + 1][c + 1] = ret[r][c + 1] + ret[r + 1][c] - ret[r][c] + a[r][c]
		return ret

	def get(self, r1, r2, c1, c2):
		if r1 < 0:
			r1 = 0
		if r2 >= self.K * 2:
			r2 = self.K * 2
		if c1 < 0:
			c1 = 0
		if c2 >= self.K * 2:
			c2 = self.K * 2
		acc2D = self.acc2D
		return acc2D[r2][c2] - acc2D[r1][c2] - acc2D[r2][c1] + acc2D[r1][c1]

def main():
	import sys
	input = sys.stdin.readline
	(N, K) = map(int, input().split())
	g = [[0] * (K * 2) for _ in range(K * 2)]
	for _ in range(N):
		(x, y, c) = input().split()
		x = int(x) % (K * 2)
		y = (int(y) - (K if c == 'B' else 0)) % (K * 2)
		g[x][y] += 1
	acc2d = Acc2D(g, K)
	ans = -1
	for r in range(K):
		for c in range(K):
			t = acc2d.get(0, r, 0, c)
			t += acc2d.get(0, r, c + K, c + K * 2)
			t += acc2d.get(r, r + K, c, c + K)
			t += acc2d.get(r + K, r + K * 2, 0, c)
			t += acc2d.get(r + K, r + K * 2, c + K, c + K * 2)
			ans = max(ans, t, N - t)
	print(ans)
main()


def main():
	(n, k) = map(int, input().split())
	e = [[0] * (2 * k + 1) for i in range(2 * k + 1)]
	mod = 2 * k
	for i in range(n):
		(x, y, c) = input().split()
		if c == 'W':
			x = int(x) % mod
			y = (int(y) + k) % mod
		else:
			x = int(x) % mod
			y = int(y) % mod
		e[x + 1][y + 1] += 1
	for i in range(mod):
		for j in range(mod):
			e[i + 1][j + 1] += e[i][j + 1] + e[i + 1][j] - e[i][j]

	def count(ax, ay, bx, by):
		return e[ax][ay] + e[bx][by] - e[ax][by] - e[bx][ay]
	ans = 0
	for i in range(k + 1):
		for j in range(k + 1):
			c = count(2 * k, 2 * k, i + k, j + k) + count(2 * k, j, i + k, 0) + count(i, 2 * k, 0, j + k) + count(i, j, 0, 0) + count(i + k, j + k, i, j)
			ans = max(ans, c, n - c)
	print(ans)
main()


(N, K) = map(int, input().split())
x = [0 for i in range(N)]
y = [0 for i in range(N)]
for i in range(N):
	(a, b, c) = map(str, input().split())
	a = int(a)
	b = int(b)
	if c == 'W':
		b += K
	x[i] = a % (2 * K)
	y[i] = b % (2 * K)
board = [[0 for i in range(2 * K + 1)] for i in range(2 * K + 1)]
for i in range(N):
	if x[i] >= K and y[i] >= K:
		x[i] -= K
		y[i] -= K
	if x[i] < K and y[i] >= K:
		x[i] += K
		y[i] -= K
	if x[i] >= K and y[i] < K:
		board[0][y[i] + 1] += 1
		board[0][y[i] + K + 1] -= 1
		board[x[i] - K + 1][y[i] + 1] -= 2
		board[x[i] - K + 1][y[i] + K + 1] += 2
		board[x[i] - K + 1][0] += 1
		board[x[i] + 1][0] -= 1
		board[x[i] + 1][y[i] + 1] += 2
		board[x[i] + 1][y[i] + K + 1] -= 2
	if x[i] < K and y[i] < K:
		board[x[i] + 1][y[i] + 1] += 2
		board[x[i] + K + 1][y[i] + K + 1] += 2
		board[x[i] + 1][y[i] + K + 1] -= 2
		board[x[i] + K + 1][y[i] + 1] -= 2
		board[0][0] += 1
		board[x[i] + 1][0] -= 1
		board[0][y[i] + 1] -= 1
		board[x[i] + K + 1][0] += 1
		board[0][y[i] + K + 1] += 1
tmp = [[0 for i in range(2 * K)] for i in range(2 * K)]
bs = [[0 for i in range(2 * K)] for i in range(2 * K)]
for i in range(2 * K):
	for j in range(2 * K):
		if i == 0:
			tmp[i][j] = board[i][j]
		else:
			tmp[i][j] = board[i][j] + tmp[i - 1][j]
for i in range(2 * K):
	for j in range(2 * K):
		if j == 0:
			bs[i][j] = tmp[i][j]
		else:
			bs[i][j] = tmp[i][j] + bs[i][j - 1]
ans = bs[0][0]
for i in range(2 * K):
	for j in range(2 * K):
		if bs[i][j] > ans:
			ans = bs[i][j]
print(ans)


(n, k) = map(int, input().split())
info = [list(input().split()) for i in range(n)]
cnt_b = [[0] * k for i in range(k)]
cnt_w = [[0] * k for i in range(k)]
for i in range(n):
	(x, y, c) = info[i]
	(x, y) = (int(x), int(y))
	move_cnt = x // k + y // k
	if move_cnt % 2 == 0:
		if c == 'B':
			cnt_b[x % k][y % k] += 1
		if c == 'W':
			cnt_w[x % k][y % k] += 1
	else:
		if c == 'B':
			cnt_w[x % k][y % k] += 1
		if c == 'W':
			cnt_b[x % k][y % k] += 1
ruiseki_b = [[0] * (k + 1) for i in range(k + 1)]
ruiseki_w = [[0] * (k + 1) for i in range(k + 1)]
for i in range(k):
	for j in range(k):
		ruiseki_b[i + 1][j + 1] = ruiseki_b[i + 1][j] + cnt_b[i][j]
		ruiseki_w[i + 1][j + 1] = ruiseki_w[i + 1][j] + cnt_w[i][j]
for j in range(k):
	for i in range(k):
		ruiseki_b[i + 1][j + 1] += ruiseki_b[i][j + 1]
		ruiseki_w[i + 1][j + 1] += ruiseki_w[i][j + 1]
ans = 0
for i in range(k + 1):
	for j in range(k + 1):
		ans_b = ruiseki_b[k][k] - ruiseki_b[i][k] - ruiseki_b[k][j] + 2 * ruiseki_b[i][j]
		ans_w = ruiseki_w[i][k] + ruiseki_w[k][j] - 2 * ruiseki_w[i][j]
		ans = max(ans_w + ans_b, ans)
		ans_w = ruiseki_w[k][k] - ruiseki_w[i][k] - ruiseki_w[k][j] + 2 * ruiseki_w[i][j]
		ans_b = ruiseki_b[i][k] + ruiseki_b[k][j] - 2 * ruiseki_b[i][j]
		ans = max(ans_w + ans_b, ans)
print(ans)


import numpy as np

def main():
	(n, k) = map(int, input().split())
	p = [list(map(str, input().split())) for _ in range(n)]
	arr = [[[0] * 2 for j in range(k)] for i in range(k)]
	for i in range(n):
		(x, y, c) = p[i]
		(x, y) = (int(x), int(y))
		(mx, my) = (x % k, y % k)
		f = False
		if x // k % 2 != y // k % 2:
			f = not f
		if c == 'W':
			f = not f
		if f:
			arr[0][0][0] += 1
			arr[mx][0][0] -= 1
			arr[0][my][0] -= 1
			arr[mx][my][0] += 2
		else:
			arr[0][my][0] += 1
			arr[mx][0][0] += 1
			arr[mx][my][0] -= 2
		f = False
		if x // k % 2 != y // k % 2:
			f = not f
		if c == 'B':
			f = not f
		if f:
			arr[0][0][1] += 1
			arr[mx][0][1] -= 1
			arr[0][my][1] -= 1
			arr[mx][my][1] += 2
		else:
			arr[0][my][1] += 1
			arr[mx][0][1] += 1
			arr[mx][my][1] -= 2
	np_arr = np.array(arr)
	np_arr = np.cumsum(np_arr, axis=0)
	np_arr = np.cumsum(np_arr, axis=1)
	print(int(np.max(np_arr)))

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
X = []
for i in range(N):
	(h, w, c) = input().split()
	(h, w) = (int(h) - 1, int(w) - 1)
	if c == 'W':
		h += K
	X.append((h % (2 * K), w % (2 * K)))

class Ruisekiwa2D:

	def __init__(self, h, w):
		self.H = h
		self.W = w
		self.G = [[0] * w for _ in range(h)]

	def load(self, points):
		for (x, y) in points:
			self.G[x][y] += 1

	def load_imos(self, points):
		for (i1, j1, i2, j2) in points:
			self.G[i1][j1] += 1
			if j2 + 1 < self.W:
				self.G[i1][j2 + 1] -= 1
			if i2 + 1 < self.H:
				self.G[i2 + 1][j1] -= 1
			if j2 + 1 < self.W and i2 + 1 < self.H:
				self.G[i2 + 1][j2 + 1] += 1

	def calc(self):
		for y in range(self.W):
			for x in range(1, self.H):
				self.G[x][y] += self.G[x - 1][y]
		for y in range(1, self.W):
			for x in range(self.H):
				self.G[x][y] += self.G[x][y - 1]

	def get(self, i1, j1, i2, j2):
		if i1 > i2 or j1 > j2 or i1 < 0 or (j1 < 0) or (i2 < 0) or (j2 < 0):
			return 0
		elif i1 > 0 and j1 > 0:
			return self.G[i2][j2] - self.G[i1 - 1][j2] - self.G[i2][j1 - 1] + self.G[i1 - 1][j1 - 1]
		elif i1 <= 0 < j1:
			return self.G[i2][j2] - self.G[i2][j1 - 1]
		elif j1 <= 0 < i1:
			return self.G[i2][j2] - self.G[i1 - 1][j2]
		else:
			return self.G[i2][j2]
R2D = Ruisekiwa2D(2 * K, 2 * K)
R2D.load(X)
R2D.calc()
ans = 0
for h in range(K):
	for w in range(K):
		tmp = 0
		tmp += R2D.get(h, w, h + K - 1, w + K - 1)
		tmp += R2D.get(0, 0, h - 1, w - 1)
		tmp += R2D.get(0, w + K, h - 1, 2 * K - 1)
		tmp += R2D.get(h + K, 0, 2 * K - 1, w - 1)
		tmp += R2D.get(h + K, w + K, 2 * K - 1, 2 * K - 1)
		ans = max(ans, tmp, N - tmp)
print(ans)


(n, k) = map(int, input().split())
(X, Y, C) = ([], [], [])
for _ in range(n):
	(x, y, c) = input().split()
	X.append(int(x) % (2 * k))
	Y.append(int(y) % (2 * k))
	C.append(c == 'B')
G = [[0] * k for _ in range(k)]
cnt = 0
for (x, y, c) in zip(X, Y, C):
	if x >= k and y < k or (x < k and y >= k):
		x += k
		c ^= 1
	x %= k
	y %= k
	G[x][y] += (-1) ** c
	cnt += c
for i in range(k):
	for j in range(k - 1):
		G[i][j + 1] += G[i][j]
for j in range(k):
	for i in range(k - 1):
		G[i + 1][j] += G[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		temp = G[k - 1][k - 1] - G[i][k - 1] - G[k - 1][j] + G[i][j] * 2 + cnt
		ans = max(ans, temp, n - temp)
print(ans)


def main():
	(n, k) = map(int, input().split())
	mp1 = [[0] * k for _ in range(k)]
	mp2 = [[0] * k for _ in range(k)]
	for _ in range(n):
		(x, y, c) = input().split()
		t = int(x) // k + int(y) // k
		x = int(x) % k
		y = int(y) % k
		if c == 'B':
			t += 1
		if t % 2:
			mp1[y][x] += 1
		else:
			mp2[y][x] += 1
	mp10 = mp1[0]
	mp20 = mp2[0]
	for x in range(1, k):
		mp10[x] += mp10[x - 1]
		mp20[x] += mp20[x - 1]
	for y in range(1, k):
		acc1 = 0
		acc2 = 0
		mp1y = mp1[y]
		mp2y = mp2[y]
		mp1y1 = mp1[y - 1]
		mp2y1 = mp2[y - 1]
		for x in range(k):
			acc1 += mp1y[x]
			mp1y[x] = acc1 + mp1y1[x]
			acc2 += mp2y[x]
			mp2y[x] = acc2 + mp2y1[x]
	ans = 0
	sum1 = mp1[k - 1][k - 1]
	sum2 = mp2[k - 1][k - 1]
	mp1k = mp1[k - 1]
	mp2k = mp2[k - 1]
	for y in range(k):
		mp1y = mp1[y]
		mp1yk = mp1y[k - 1]
		mp2y = mp2[y]
		mp2yk = mp2y[k - 1]
		for x in range(k):
			mp1yx = mp1y[x]
			mp2yx = mp2y[x]
			mp1kx = mp1k[x]
			mp2kx = mp2k[x]
			a1 = 2 * mp1yx + sum1 - mp1yk - mp1kx + mp2kx + mp2yk - 2 * mp2yx
			a2 = 2 * mp2yx + sum2 - mp2yk - mp2kx + mp1kx + mp1yk - 2 * mp1yx
			if ans < a1:
				ans = a1
			if ans < a2:
				ans = a2
	print(ans)
main()


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
C = [list(input().split()) for i in range(n)]
D = [[0] * (2 * k) for i in range(2 * k)]
E = []
for i in range(n):
	x = int(C[i][0]) % (2 * k)
	y = int(C[i][1]) % (2 * k)
	if C[i][2] == 'W':
		x = (x + k) % (2 * k)
	E.append([x, y])
	x = (x + k) % (2 * k)
	y = (y + k) % (2 * k)
	E.append([x, y])
for i in range(2 * n):
	xx = 0
	yy = 0
	x = E[i][0]
	y = E[i][1]
	D[x][y] += 1
	if x + k > 2 * k:
		xx = 1
	if y + k > 2 * k:
		yy = 1
	if xx == 1 and yy == 1:
		D[0][0] += 1
		D[x - k][0] -= 1
		D[0][y - k] -= 1
		D[x - k][y - k] += 1
		D[x][0] += 1
		D[x][y - k] -= 1
		D[0][y] += 1
		D[x - k][y] -= 1
	elif xx == 1:
		D[0][y] += 1
		D[x - k][y] -= 1
		if y != k:
			D[x][y + k] -= 1
			D[0][y + k] -= 1
			D[x - k][y + k] += 1
	elif yy == 1:
		D[x][0] += 1
		D[x][y - k] -= 1
		if x != k:
			D[x + k][y] -= 1
			D[x + k][0] -= 1
			D[x + k][y - k] += 1
	else:
		if x != k:
			D[x + k][y] -= 1
		if y != k:
			D[x][y + k] -= 1
		if x != k and y != k:
			D[x + k][y + k] += 1
for i in range(1, 2 * k):
	D[0][i] += D[0][i - 1]
	D[i][0] += D[i - 1][0]
for i in range(1, 2 * k):
	for j in range(1, 2 * k):
		D[i][j] += D[i - 1][j] + D[i][j - 1] - D[i - 1][j - 1]
print(max([max(D[i]) for i in range(2 * k)]))


(N, K) = map(int, input().split())
(x, y, c) = ([None] * N, [None] * N, [None] * N)
KK = 2 * K
table = [[0] * (KK + 1) for i in range(KK + 1)]
for i in range(N):
	(x[i], y[i], c[i]) = input().split()
	(x[i], y[i]) = (int(x[i]), int(y[i]))
	if c[i] == 'W':
		x[i] += K
		c[i] = 'B'
	x[i] %= KK
	y[i] %= KK
	table[y[i] + 1][x[i] + 1] += 1
for i in range(KK + 1):
	for j in range(KK):
		table[i][j + 1] += table[i][j]
for i in range(KK):
	for j in range(KK + 1):
		table[i + 1][j] += table[i][j]

def s(x1, x2, y1, y2):
	global table, KK
	MOD = KK + 1
	x1 = x1 % MOD + x1 // MOD
	y1 = y1 % MOD + y1 // MOD
	x2 = x2 % MOD + x2 // MOD
	y2 = y2 % MOD + y2 // MOD
	x11 = x1
	x21 = 0
	y11 = y1
	y21 = 0
	if x1 < x2:
		x12 = x2
		x22 = 0
	else:
		x12 = KK
		x22 = x2
	if y1 < y2:
		y12 = y2
		y22 = 0
	else:
		y12 = KK
		y22 = y2
	f = lambda _x1, _x2, _y1, _y2: table[_y2][_x2] - table[_y2][_x1] - table[_y1][_x2] + table[_y1][_x1]
	return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)
ans = 0
for i in range(KK):
	for j in range(K):
		tmp = s(i, i + K, j, j + K)
		(ni, nj) = (i + K, j + K)
		tmp += s(ni, ni + K, nj, nj + K)
		ans = max((ans, tmp))
print(ans)


from itertools import product
(N, K) = map(int, input().split())
kk = K * 2

class CumSum2d(object):

	def __init__(self, H, W):
		(self.H, self.W) = (H, W)
		self.tiles = [[0] * W for _ in range(H)]

	def add(self, x, y, c=1):
		self.tiles[y][x] += c

	def build(self):
		for y in range(self.H):
			for x in range(1, self.W):
				self.tiles[y][x] += self.tiles[y][x - 1]
		for y in range(1, self.H):
			for x in range(self.W):
				self.tiles[y][x] += self.tiles[y - 1][x]

	def get(self, sx, sy, tx, ty):
		assert sx <= tx and sy <= ty
		cnt = self.tiles[ty][tx]
		if sx > 0:
			cnt -= self.tiles[ty][sx - 1]
		if sy > 0:
			cnt -= self.tiles[sy - 1][tx]
		if sx > 0 and sy > 0:
			cnt += self.tiles[sy - 1][sx - 1]
		return cnt
cs = CumSum2d(kk, kk)
for i in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'B':
		y += K
	cs.add(x % kk, y % kk)
cs.build()
ans = 0
for (x, y) in product(range(K), repeat=2):
	cnt = cs.get(x, y, x + K - 1, y + K - 1)
	cnt += cs.get(x + K, y + K, kk - 1, kk - 1)
	if x > 0:
		cnt += cs.get(0, y + K, x - 1, kk - 1)
	if y > 0:
		cnt += cs.get(x + K, 0, kk - 1, y - 1)
	if x > 0 and y > 0:
		cnt += cs.get(0, 0, x - 1, y - 1)
	ans = max(ans, cnt, N - cnt)
print(ans)


(n, k) = map(int, input().split())
m = 2 * k
a = [(m + 1) * [0] for _ in [0] * (m + 1)]
z = lambda i, j: (a[i][j] - a[i - k][j] - a[i][j - k] + a[i - k][j - k]) * 2 + a[m][m] - a[i][m] - a[m][j] + a[i - k][m] + a[m][j - k]
for _ in [0] * n:
	(x, y, c) = input().split()
	x = int(x)
	y = int(y) + k * (c == 'B')
	a[x % m + 1][y % m + 1] += 1
for i in range(m):
	for j in range(m):
		a[i + 1][j + 1] += a[i][j + 1] + a[i + 1][j] - a[i][j]
print(max((max((max(z(i, j), n - z(i, j)) for j in range(k, m + 1))) for i in range(k, m + 1))))


import sys
input = sys.stdin.readline
(N, K) = list(map(int, input().split()))
data = [list(map(str, input().split())) for _ in range(N)]
base = 10 ** 6
BW_data = [[0 for i in range(2 * K)] for i in range(2 * K)]
for (x, y, c) in data:
	(x, y) = (int(x) % (2 * K), int(y) % (2 * K))
	if c == 'B':
		BW_data[y][x] += 1
	else:
		BW_data[y][x] += base

def ACC2(l):
	H = len(l)
	W = len(l[0])
	ret = [[0] * (W + 1) for i in range(H + 1)]
	for i in range(H):
		for j in range(W):
			ret[i + 1][j + 1] = ret[i + 1][j] + ret[i][j + 1] - ret[i][j] + l[i][j]
	return ret

def RANGE_SUM_in_ACC(l, h1, h2, w1, w2):
	return l[h2][w2] - l[h2][w1] - l[h1][w2] + l[h1][w1]
BW_ACC = ACC2(BW_data)
BW_res = [[0 for i in range(2 * K)] for i in range(K)]
for j in range(K):
	for i in range(2 * K):
		if i < K:
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, j, j + K, i, i + K)
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, 0, max(0, j), 0, max(0, i))
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, 0, max(0, j), min(2 * K, i + K), 2 * K)
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, min(2 * K, j + K), 2 * K, 0, max(0, i))
			BW_res[j][i] += RANGE_SUM_in_ACC(BW_ACC, min(2 * K, j + K), 2 * K, min(2 * K, i + K), 2 * K)
		else:
			BW_res[j][i] = BW_ACC[-1][-1] - BW_res[j][i - K]
ans = 0
for j in range(K):
	for i in range(2 * K):
		ans = max(ans, BW_res[j][i] % base + BW_res[j][(i + K) % (2 * K)] // base)
print(ans)


import sys
(n, k) = map(int, sys.stdin.readline().strip().split())
x = []
y = []
mx = []
my = []
for _ in range(n):
	(xx, yy, c) = sys.stdin.readline().strip().split()
	xx = int(xx)
	yy = int(yy)
	if c == 'W':
		xx += k
	xx %= 2 * k
	yy %= 2 * k
	if xx >= k:
		if yy >= k:
			x.append(xx - k)
			y.append(yy - k)
		else:
			mx.append(xx - k)
			my.append(yy)
	elif yy >= k:
		mx.append(xx)
		my.append(yy - k)
	else:
		x.append(xx)
		y.append(yy)
graph = [[0 for _ in range(k)] for __ in range(k)]
lx = len(x)
graph[0][0] += lx
for i in range(lx):
	graph[x[i]][0] -= 1
	graph[0][y[i]] -= 1
	graph[x[i]][y[i]] += 2
lmx = len(mx)
graph[0][0] -= lmx
for i in range(lmx):
	graph[mx[i]][0] += 1
	graph[0][my[i]] += 1
	graph[mx[i]][my[i]] -= 2
for i in graph:
	for j in range(k - 1):
		i[j + 1] += i[j]
for i in range(k):
	for j in range(k - 1):
		graph[j + 1][i] += graph[j][i]
big = max([max(i) for i in graph]) + lmx
small = min([min(i) for i in graph]) - lx
if big + small >= 0:
	print(big)
else:
	print(-small)


(n, k) = map(int, input().split())
xy = []
F = [[0] * (2 * k + 1) for _ in range(2 * k + 1)]
for __ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'B':
		y += k
	x %= 2 * k
	y %= 2 * k
	for _ in range(2):
		x = (x + k) % (2 * k)
		y = (y + k) % (2 * k)
		F[y + 1][x + 1] += 1
		if x - k >= 0 and y - k >= 0:
			F[y + 1][x - k + 1] -= 1
			F[y - k + 1][x + 1] -= 1
			F[y - k + 1][x - k + 1] += 1
		elif y - k >= 0:
			F[y - k + 1][0] += 1
			F[y + 1][0] -= 1
			F[y - k + 1][x + 1] -= 1
			F[y - k + 1][x + k + 1] += 1
			F[y + 1][x + k + 1] -= 1
		elif x - k >= 0:
			F[0][x - k + 1] += 1
			F[0][x + 1] -= 1
			F[y + 1][x - k + 1] -= 1
			F[y + k + 1][x - k + 1] += 1
			F[y + k + 1][x + 1] -= 1
		else:
			F[0][x + 1] -= 1
			F[y + 1][0] -= 1
			F[0][0] += 1
			F[y + k + 1][0] += 1
			F[y + k + 1][x + 1] -= 1
			F[0][x + k + 1] += 1
			F[y + 1][x + k + 1] -= 1
			F[y + k + 1][x + k + 1] += 1
	if __ == 0:
		pass
ans = 0
m = 3
from numpy import array
F = array(F)
for i in range(0, 2 * k):
	F[:, i + 1] += F[:, i]
for i in range(0, 2 * k):
	F[i + 1, :] += F[i, :]
print(F[:2 * k, :2 * k].max())


import numpy as np
(N, K) = list(map(int, input().split()))
B = np.zeros((2 * K, 2 * K), np.int32)
W = np.zeros((2 * K, 2 * K), np.int32)
for _ in range(N):
	(x, y, c) = input().split()
	if c == 'B':
		B[int(x) % (2 * K)][int(y) % (2 * K)] += 1
	else:
		W[int(x) % (2 * K)][int(y) % (2 * K)] += 1
SB = np.zeros((4 * K + 1, 4 * K + 1), np.int32)
SW = np.zeros((4 * K + 1, 4 * K + 1), np.int32)
SB[0 * K + 1:2 * K + 1, 0 * K + 1:2 * K + 1] = B
SB[0 * K + 1:2 * K + 1, 2 * K + 1:4 * K + 1] = B
SB[2 * K + 1:4 * K + 1, 0 * K + 1:2 * K + 1] = B
SB[2 * K + 1:4 * K + 1, 2 * K + 1:4 * K + 1] = B
SW[0 * K + 1:2 * K + 1, 0 * K + 1:2 * K + 1] = W
SW[0 * K + 1:2 * K + 1, 2 * K + 1:4 * K + 1] = W
SW[2 * K + 1:4 * K + 1, 0 * K + 1:2 * K + 1] = W
SW[2 * K + 1:4 * K + 1, 2 * K + 1:4 * K + 1] = W
np.cumsum(SB, 0, out=SB)
np.cumsum(SB, 1, out=SB)
np.cumsum(SW, 0, out=SW)
np.cumsum(SW, 1, out=SW)
ans = 0
cand = SB[1 * K:3 * K + 1, 1 * K:3 * K + 1].copy()
cand -= SB[0 * K:2 * K + 1, 1 * K:3 * K + 1]
cand -= SB[1 * K:3 * K + 1, 0 * K:2 * K + 1]
cand += SB[0 * K:2 * K + 1, 0 * K:2 * K + 1]
cand += SB[2 * K:4 * K + 1, 2 * K:4 * K + 1]
cand -= SB[1 * K:3 * K + 1, 2 * K:4 * K + 1]
cand -= SB[2 * K:4 * K + 1, 1 * K:3 * K + 1]
cand += SB[1 * K:3 * K + 1, 1 * K:3 * K + 1]
cand += SW[2 * K:4 * K + 1, 1 * K:3 * K + 1]
cand -= SW[1 * K:3 * K + 1, 1 * K:3 * K + 1]
cand -= SW[2 * K:4 * K + 1, 0 * K:2 * K + 1]
cand += SW[1 * K:3 * K + 1, 0 * K:2 * K + 1]
cand += SW[1 * K:3 * K + 1, 2 * K:4 * K + 1]
cand -= SW[0 * K:2 * K + 1, 2 * K:4 * K + 1]
cand -= SW[1 * K:3 * K + 1, 1 * K:3 * K + 1]
cand += SW[0 * K:2 * K + 1, 1 * K:3 * K + 1]
ans = np.max(cand)
print(ans)


import copy
from datetime import datetime
(n, k) = map(int, input().split())
p = [[0] * (2 * k) for i in range(2 * k)]
ans = 0
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'B':
		x += k
	p[x % (2 * k)][y % (2 * k)] += 1
csum1 = [[0] * (2 * k) for i in range(2 * k)]
for i in range(2 * k):
	for j in range(0, k):
		csum1[i][0] += p[i][j]
	for j in range(1, 2 * k):
		csum1[i][j] = csum1[i][j - 1] - p[i][j - 1] + p[i][(j + k - 1) % (2 * k)]
csum2 = [[0] * (2 * k) for i in range(2 * k)]
for j in range(2 * k):
	for i in range(0, k):
		csum2[0][j] += csum1[i][j]
	for i in range(1, 2 * k):
		csum2[i][j] = csum2[i - 1][j] - csum1[i - 1][j] + csum1[(i + k - 1) % (2 * k)][j]
for i in range(2 * k):
	for j in range(2 * k):
		ans = max(ans, csum2[i][j] + csum2[(i + k) % (2 * k)][(j + k) % (2 * k)])
print(ans)


(N, K) = map(int, input().split())
grid = [[0 for _ in range(2 * K)] for _ in range(2 * K)]
for _ in range(N):
	(x, y, c) = input().split()
	(i, j) = (int(x), int(y))
	if c == 'B':
		j += K
	i %= 2 * K
	j %= 2 * K
	grid[i][j] += 1
for i in range(2 * K):
	for j in range(2 * K):
		if i == 0:
			if j != 0:
				grid[i][j] += grid[i][j - 1]
		elif j == 0:
			grid[i][j] += grid[i - 1][j]
		else:
			grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1]
ans = 0
M = grid[2 * K - 1][2 * K - 1]
for i in range(K):
	for j in range(K):
		temp = 0
		c = 0
		temp += M - grid[i + K - 1][-1] - grid[-1][j + K - 1] + grid[i + K - 1][j + K - 1]
		temp += grid[i + K - 1][j + K - 1]
		if i > 0 and j == 0:
			temp += grid[i - 1][-1] - 2 * grid[i - 1][j + K - 1]
		if i == 0 and j > 0:
			temp += grid[-1][j - 1] - 2 * grid[i + K - 1][j - 1]
		if i > 0 and j > 0:
			temp += grid[i - 1][-1] - 2 * grid[i - 1][j + K - 1]
			temp += grid[-1][j - 1] - 2 * grid[i + K - 1][j - 1]
			temp += 2 * grid[i - 1][j - 1]
		ans = max(ans, temp, M - temp)
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
(n, k) = [int(i) for i in readline().split()]
xyc = []
B = [[0] * (k + 1) for i in range(k + 1)]
W = [[0] * (k + 1) for i in range(k + 1)]
for _ in range(n):
	(x, y, c) = [i for i in readline().split()]
	x = int(x)
	y = int(y)
	sgn = 0
	sgn += x // k
	x %= k
	sgn += y // k
	y %= k
	if sgn & 1:
		if c == 'W':
			B[x + 1][y + 1] += 1
		else:
			W[x + 1][y + 1] += 1
	elif c == 'W':
		W[x + 1][y + 1] += 1
	else:
		B[x + 1][y + 1] += 1
for i in range(k):
	for j in range(k):
		B[i + 1][j + 1] += B[i + 1][j] + B[i][j + 1] - B[i][j]
		W[i + 1][j + 1] += W[i + 1][j] + W[i][j + 1] - W[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		resb1 = B[i][j] + (B[-1][-1] - B[-1][j] - B[i][-1] + B[i][j])
		resw1 = W[i][j] + (W[-1][-1] - W[-1][j] - W[i][-1] + W[i][j])
		resb2 = B[i][-1] - B[i][j] + (B[-1][j] - B[i][j])
		resw2 = W[i][-1] - W[i][j] + (W[-1][j] - W[i][j])
		ans = max(ans, resb1 + resw2, resw1 + resb2)
print(ans)


from numpy import *
(N, K) = map(int, input().split())
M = zeros((3 * K, 3 * K), int)
L = 2 * K
O = 0
for n in range(N):
	(x, y, c) = input().split()
	x = int(x) % L
	y = int(y) % L
	t = c == 'W'
	M[x, y] += 1 - 2 * t
	O += t
for n in range(2):
	M[L:] = M[:K]
	M = cumsum(M, axis=0)
	M[:L] = M[K:] - M[:L]
	M = M.T
M = M[:L, :L]
print(O + (M + roll(roll(M, K, axis=0), K, axis=1)).max())


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
lis = [[[0] * K for _ in range(K)] for i in range(2)]
for i in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'B':
		C = 1
	if c == 'W':
		C = 0
	now = (x // K + y // K + C) % 2
	lis[now][x % K][y % K] += 1
for t in range(2):
	for i in range(K):
		for j in range(1, K):
			lis[t][i][j] += lis[t][i][j - 1]
	for i in range(1, K):
		for j in range(K):
			lis[t][i][j] += lis[t][i - 1][j]
ans = 0
for t in range(2):
	for i in range(K):
		for j in range(K):
			cnt = 2 * lis[t][i][j] + lis[t][-1][-1] - lis[t][i][-1] - lis[t][-1][j] + lis[1 - t][i][-1] + lis[1 - t][-1][j] - 2 * lis[1 - t][i][j]
			ans = max(ans, cnt)
print(ans)


from subprocess import *
call(('julia', '-e', '\nconst lines=readlines()\ninput()=shift!(lines)\nint(s)=parse(Int32,s)\nintLine()=map(int,split(input()))\nfunction nextLine(k)\n    x,y,c=split(input())\n    int(x)%2k+1,(int(y)+(c=="W")k)%2k+1\nend\nfunction solve(k,g)\n    m=0\n    for i=1:2k,j=1:2k\n        a=0\n        for x=-1:2,y=-1:2\n            (x+y)%2!=0&&continue\n            v,w=i+x*k,j+y*k\n            v>0<w&&(a+=g[min(2k,v)][min(2k,w)])\n            v>k&&w>0&&(a-=g[min(2k,v-k)][min(2k,w)])\n            w>k&&v>0&&(a-=g[min(2k,v)][min(2k,w-k)])\n            v>k<w&&(a+=g[min(2k,v-k)][min(2k,w-k)])\n        end\n        m=max(m,a)\n    end\n    m\nend\nfunction main()\n    n,k=intLine()\n    g=Array{Int32,1}[[0for j=1:2k]for i=1:2k]\n    for i=1:n\n        x,y=nextLine(k)\n        g[x][y]+=1\n    end\n    println(solve(k,cumsum(map(cumsum,g))))\nend\nmain()\n'))


import numpy as np
(N, K) = map(int, input().split())
_3K = 3 * K
_2K = 2 * K
G = np.zeros((_2K, _3K), dtype='int64')
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'B':
		x = x % _2K
		y = y % _2K
	else:
		x = (x + K) % _2K
		y = y % _2K
	if y >= K and x >= K:
		x -= K
		y -= K
	if y >= K and x < K:
		x += K
		y -= K
	G[y, x] += 1
	G[y + K, x + K] += 1
	G[y + K, x] -= 1
	G[y, x + K] -= 1
G = G.cumsum(axis=1).cumsum(axis=0)
G = G[:K, :_2K] + G[K:_2K, K:_3K] + np.concatenate((G[:K, _2K:_3K], G[K:_2K, :K]), axis=1)
ans = G.max()
print(ans)


(N, K) = map(int, input().split())
B = [[0] * (2 * K) for i in range(2 * K)]
W = [[0] * (2 * K) for i in range(2 * K)]
w = 0
for i in range(N):
	(x, y, c) = map(str, input().split())
	x = int(x)
	y = int(y)
	if c == 'B':
		B[y % (2 * K)][x % (2 * K)] += 1
	else:
		w += 1
		W[y % (2 * K)][x % (2 * K)] += 1
sumb = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
sumw = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
for i in range(2 * K):
	for j in range(2 * K):
		sumb[i + 1][j + 1] = sumb[i][j + 1] + sumb[i + 1][j] - sumb[i][j] + B[i][j]
		sumw[i + 1][j + 1] = sumw[i][j + 1] + sumw[i + 1][j] - sumw[i][j] + W[i][j]
ans = 0
for i in range(K):
	for j in range(K):
		for _ in range(2):
			s = 0
			s += sumb[i][j] - sumb[i][0] - sumb[0][j] + sumb[0][0]
			s += sumb[i + K][j + K] - sumb[i][j + K] - sumb[i + K][j] + sumb[i][j]
			s += sumb[i][2 * K] - sumb[0][2 * K] - sumb[i][j + K] + sumb[0][j + K]
			s += sumb[2 * K][j] - sumb[i + K][j] - sumb[2 * K][0] + sumb[i + K][0]
			s += sumb[2 * K][2 * K] - sumb[i + K][2 * K] - sumb[2 * K][j + K] + sumb[i + K][j + K]
			s += sumw[i][j + K] - sumw[0][j + K] - sumw[i][j] + sumw[0][j]
			s += sumw[i + K][j] - sumw[i][j] - sumw[i + K][0] + sumw[i][0]
			s += sumw[2 * K][j + K] - sumw[i + K][j + K] - sumw[2 * K][j] + sumw[i + K][j]
			s += sumw[i + K][2 * K] - sumw[i + K][j + K] - sumw[i][2 * K] + sumw[i][j + K]
			ans = max(ans, s)
			(sumb, sumw) = (sumw, sumb)
print(ans)


import numpy as np
import sys

def sum2d(i1, j1, i2, j2):
	A[i1][j1] += 1
	A[i1][j2] -= 1
	A[i2][j1] -= 1
	A[i2][j2] += 1
(N, K) = list(map(int, input().split()))
XYC = [list(input().split()) for _ in range(N)]
A = [[0 for _ in range(K * 2 + 1)] for _ in range(K * 2 + 1)]
for (x, y, c) in XYC:
	i = (int(x) if c == 'W' else int(x) + K) % (K * 2)
	j = int(y) % (K * 2)
	i0 = i % K
	j0 = j % K
	if i < K and j < K or (i >= K and j >= K):
		sum2d(0, 0, i0, j0)
		sum2d(i0 + K, 0, K * 2, j0)
		sum2d(i0, j0, i0 + K, j0 + K)
		sum2d(0, j0 + K, i0, K * 2)
		sum2d(i0 + K, j0 + K, K * 2, K * 2)
	else:
		sum2d(i0, 0, i0 + K, j0)
		sum2d(0, j0, i0, j0 + K)
		sum2d(i0 + K, j0, K * 2, j0 + K)
		sum2d(i0, j0 + K, i0 + K, K * 2)
AA = np.array(A)
for i in range(K * 2):
	AA[i + 1, :] += AA[i, :]
for j in range(K * 2):
	AA[:, j + 1] += AA[:, j]
print(AA.max())


ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
INF = 10 ** 18
(N, K) = rl()
W = [[0 for _ in range(K + 3)] for _ in range(K + 3)]
for i in range(N):
	(x, y, c) = input().split()
	x = int(x) % (2 * K)
	y = (int(y) if c == 'W' else int(y) + K) % (2 * K)
	if (x < K) ^ (y < K):
		x %= K
		y %= K
		W[0][y + 1] += 1
		W[x + 1][y + 1] -= 2
		W[0][K + 1] -= 1
		W[x + 1][K + 1] += 1
		W[x + 1][0] += 1
		W[K + 1][0] -= 1
		W[K + 1][y + 1] += 1
	else:
		x %= K
		y %= K
		W[0][0] += 1
		W[x + 1][0] -= 1
		W[0][y + 1] -= 1
		W[x + 1][y + 1] += 2
		W[K + 1][y + 1] -= 1
		W[x + 1][K + 1] -= 1
		W[K + 1][K + 1] += 1
for x in range(0, K + 2):
	for y in range(0, K + 2):
		W[x][y] += W[x][y - 1]
for y in range(0, K + 2):
	for x in range(0, K + 2):
		W[x][y] += W[x - 1][y]
ans = 0
for x in range(K + 1):
	for y in range(K + 1):
		res = W[x][y]
		ans = max(ans, res, N - res)
print(ans)


def cumsum(T, K):
	R = [[0] * K for _ in range(K)]
	for i in range(K):
		t = 0
		for j in range(K):
			t += T[i][j]
			R[i][j] = t
	for i in range(K):
		t = 0
		for j in range(K):
			t += R[j][i]
			R[j][i] = t
	return R

def fm(B, W, K):
	m = 0
	for i in range(K):
		for j in range(K):
			t = B[i][j]
			if i == K - 1 or j == K - 1:
				t += W[K - 1][K - 1] - W[i][j]
			else:
				t += B[K - 1][K - 1] - B[K - 1][j] - B[i][K - 1] + B[i][j]
				t += W[K - 1][j] + W[i][K - 1] - 2 * W[i][j]
			m = max(m, t)
	return m

def main():
	(N, K) = map(int, input().split())
	K2 = 2 * K
	B = [[0] * K for _ in range(K)]
	W = [[0] * K for _ in range(K)]
	for _ in range(N):
		(x1, y1, c) = input().split()
		(x, y) = (int(x1) % K2, int(y1) % K2)
		if c == 'B':
			x = (x - K) % K2
		if x >= K and y >= K:
			(x, y) = (x - K, y - K)
		if x < K and y < K:
			B[x][y] += 1
		else:
			(x, y) = (x - K, y) if y < K else (x, y - K)
			W[x][y] += 1
	B = cumsum(B, K)
	W = cumsum(W, K)
	return max(fm(B, W, K), fm(W, B, K))
print(main())


import sys
(N, K) = map(int, input().split())
cnt_B = 0
black = [[0] * (2 * K) for _ in range(2 * K)]
for s in sys.stdin.readlines():
	(x, y, c) = s.split()
	x = int(x)
	y = int(y)
	y = y if c == 'B' else y + K
	black[x % (2 * K)][y % (2 * K)] += 1
	cnt_B += 1
S_black = [[0] * (2 * K + 1) for _ in range(2 * K)]
for i in range(2 * K):
	for j in range(2 * K):
		S_black[i][j + 1] = S_black[i][j] + black[i][j]
ans = 0
for j in range(K):
	var_B = 0
	for i in range(K):
		var_B += S_black[i][j + K] - S_black[i][j]
		var_B += S_black[i + K][2 * K] - (S_black[i + K][j + K] - S_black[i + K][j])
	ans = max(ans, max(var_B, cnt_B - var_B))
	for i in range(K):
		var_B += S_black[i + K][j + K] - S_black[i + K][j] - (S_black[i][j + K] - S_black[i][j])
		var_B -= S_black[i + K][2 * K] - (S_black[i + K][j + K] - S_black[i + K][j])
		var_B += S_black[i][2 * K] - (S_black[i][j + K] - S_black[i][j])
		ans = max(ans, max(var_B, cnt_B - var_B))
print(ans)


def main():
	(n, k) = map(int, input().split())
	grid = [[0] * k for _ in range(k)]
	for i in range(n):
		(x, y, c) = map(lambda x: x, input().split())
		(x, y) = (int(x), int(y))
		p = (x // k + y // k) % 2
		if (p == 0 and c == 'B') | (p == 1 and c == 'W'):
			grid[x % k][y % k] += 1
		else:
			grid[x % k][y % k] -= 1
	for i in range(k):
		for j in range(1, k):
			grid[i][j] += grid[i][j - 1]
	for i in range(1, k):
		for j in range(k):
			grid[i][j] += grid[i - 1][j]
	ans = 0
	for i in range(k):
		for j in range(k):
			b = grid[i][j] * 4 + grid[k - 1][k - 1] - (grid[i][k - 1] + grid[k - 1][j]) * 2
			ans = max(ans, n + b, n - b)
	print(ans // 2)
main()


import numpy as np
import sys
(N, K) = map(int, input().split())
K2 = K * 2
Mij = [[0 for _ in range(K2 + 1)] for _ in range(K2 + 1)]

def imos(i1, j1, i2, j2):
	Mij[i1][j1] += 1
	Mij[i1][j2] -= 1
	Mij[i2][j1] -= 1
	Mij[i2][j2] += 1
for _ in range(N):
	(i, j, s) = sys.stdin.readline().split()
	i = int(i) % K2
	j = int(j) % K2
	if s == 'B':
		i = (i + K) % K2
	i1 = i % K
	j1 = j % K
	if i < K and j < K or (K <= i and K <= j):
		imos(0, 0, i1, j1)
		imos(0, j1 + K, i1, K2)
		imos(i1 + K, 0, K2, j1)
		imos(i1 + K, j1 + K, K2, K2)
		imos(i1, j1, i1 + K, j1 + K)
	else:
		imos(0, j1, i1, j1 + K)
		imos(i1 + K, j1, K2, j1 + K)
		imos(i1, 0, i1 + K, j1)
		imos(i1, j1 + K, i1 + K, K2)
Mij = np.array(Mij)
for x in range(K2):
	Mij[:, x + 1] += Mij[:, x]
for y in range(K2):
	Mij[y + 1, :] += Mij[y, :]
print(Mij.max())


(n, k) = map(int, input().split())
l = [input().split() for _ in range(n)]
w = [[0] * (k + 1) for _ in range(k + 1)]
b = [[0] * (k + 1) for _ in range(k + 1)]
for i in range(n):
	if ((int(l[i][0]) // k + int(l[i][1]) // k) % 2 == 0) ^ (l[i][2] == 'W'):
		b[int(l[i][0]) % k][int(l[i][1]) % k] += 1
	else:
		w[int(l[i][0]) % k][int(l[i][1]) % k] += 1
for i in range(1, k):
	b[0][i] += b[0][i - 1]
	b[i][0] += b[i - 1][0]
	w[0][i] += w[0][i - 1]
	w[i][0] += w[i - 1][0]
for i in range(1, k):
	for j in range(1, k):
		b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]
		w[i][j] += w[i - 1][j] + w[i][j - 1] - w[i - 1][j - 1]
ans = 0
for i in range(k):
	for j in range(k):
		ans = max(ans, b[k - 1][k - 1] - b[i][k - 1] - b[k - 1][j] + 2 * b[i][j] + w[i][k - 1] + w[k - 1][j] - 2 * w[i][j])
		ans = max(ans, w[k - 1][k - 1] - w[i][k - 1] - w[k - 1][j] + 2 * w[i][j] + b[i][k - 1] + b[k - 1][j] - 2 * b[i][j])
print(ans)


(N, K) = map(int, input().split())
XYC = [input().split() for i in range(N)]
d = [[[0] * K for i in range(K)] for i in range(2)]
for (x, y, c) in XYC:
	(x, y) = (int(x), int(y))
	z = x // K % 2 ^ y // K % 2 ^ (c == 'B')
	d[z][x % K][y % K] += 1
a = [[[0] * (K + 1) for i in range(K + 1)] for i in range(2)]
for k in range(2):
	for i in range(K):
		for j in range(K):
			a[k][i + 1][j + 1] = a[k][i + 1][j] + a[k][i][j + 1] - a[k][i][j] + d[k][i][j]
r = 0
for k in range(2):
	for i in range(K):
		for j in range(K):
			r = max(r, a[k][-1][-1] - a[k][i][-1] - a[k][-1][j] + a[k][i][j] * 2 + a[k ^ 1][i][-1] + a[k ^ 1][-1][j] - a[k ^ 1][i][j] * 2)
print(r)


(N, K) = map(int, input().split())
grid = [[0] * (2 * K + 2) for i in range(2 * K + 2)]
for i in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		x += K
	grid[y % (2 * K) + 1][x % (2 * K) + 1] += 1
for i in range(2 * K + 2):
	for j in range(2 * K + 1):
		grid[i][j + 1] += grid[i][j]
for i in range(2 * K + 1):
	for j in range(2 * K + 2):
		grid[i + 1][j] += grid[i][j]

def count(x1, x2, y1, y2):
	return grid[y1][x1] - grid[y1][x2] - grid[y2][x1] + grid[y2][x2]
ans = 0
for i in range(K):
	for j in range(K):
		tmp = count(j, j + K, i, i + K) + count(0, j, 0, i) + count(j + K, 2 * K, 0, i) + count(0, j, i + K, 2 * K) + count(j + K, 2 * K, i + K, 2 * K)
		ans = max(ans, tmp, N - tmp)
print(ans)


(N, K) = map(int, input().split())
X = [input().split() for _ in range(N)]
grid = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
for (x, y, c) in X:
	(x, y) = map(int, [x, y])
	if c == 'W':
		y += K
	grid[x % (2 * K) + 1][y % (2 * K) + 1] += 1
for i in range(2 * K):
	for j in range(2 * K):
		grid[i + 1][j + 1] += grid[i][j + 1]
for i in range(2 * K):
	for j in range(2 * K):
		grid[i + 1][j + 1] += grid[i + 1][j]

def count(x, y, i, j):
	return grid[x][y] + grid[i][j] - grid[x][j] - grid[i][y]
ans = 0
for i in range(K + 1):
	for j in range(K + 1):
		tmp = 0
		tmp += count(i + K, j + K, i, j)
		tmp += count(i, j, 0, 0)
		tmp += count(i, 2 * K, 0, j + K)
		tmp += count(2 * K, j, i + K, 0)
		tmp += count(2 * K, 2 * K, i + K, j + K)
		ans = max(ans, tmp, N - tmp)
print(ans)


(N, K) = map(int, input().split())
g = [[0] * K for _ in range(K)]
for _ in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	tmp = (x // K + y // K) % 2
	x %= K
	y %= K
	if tmp == 0 and c == 'B' or (tmp == 1 and c == 'W'):
		g[x][y] += 1
	else:
		g[x][y] -= 1
for i in range(K):
	for j in range(1, K):
		g[i][j] += g[i][j - 1]
for i in range(1, K):
	for j in range(K):
		g[i][j] += g[i - 1][j]
ans = 0
for i in range(K):
	for j in range(K):
		n_satisfied = g[-1][-1] - 2 * (g[-1][j] + g[i][-1]) + 4 * g[i][j]
		ans = max(ans, (N + n_satisfied) // 2, (N - n_satisfied) // 2)
print(ans)


(n, k) = map(int, input().split())
d = [[0 for _ in range(k + 1)] for _ in range(k + 1)]
e = [[0 for _ in range(k + 1)] for _ in range(k + 1)]
(cnt_d, cnt_e) = (0, 0)
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	res = (x // k + y // k) % 2
	if res == 0 and c == 'B' or (res == 1 and c == 'W'):
		d[x % k + 1][y % k + 1] += 1
		cnt_d += 1
	else:
		e[x % k + 1][y % k + 1] += 1
		cnt_e += 1
for i in range(k + 1):
	for j in range(k):
		d[i][j + 1] += d[i][j]
		e[i][j + 1] += e[i][j]
for j in range(k + 1):
	for i in range(k):
		d[i + 1][j] += d[i][j]
		e[i + 1][j] += e[i][j]
ans = 0
for i in range(1, k + 1):
	for j in range(1, k + 1):
		res_d = d[-1][-1] - d[i - 1][-1] - d[-1][j - 1] + 2 * d[i - 1][j - 1]
		res_e = e[-1][-1] - e[i - 1][-1] - e[-1][j - 1] + 2 * e[i - 1][j - 1]
		ans = max(ans, res_d + cnt_e - res_e, res_e + cnt_d - res_d)
print(ans)


import sys
k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
	global acc, l
	if y1 > y2 or x1 > x2:
		return 0
	y2 += 1
	x2 += 1
	return acc[y2][x2] - acc[y1][x2] - acc[y2][x1] + acc[y1][x1]

def main():
	global acc, k, l
	(n, k) = map(int, sys.stdin.readline().split())
	xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x, line.split()) for line in sys.stdin.readlines())
	l = k * 2
	yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for (x, y, c) in xycs)
	acc = [[0] * (l + 1) for _ in range(l + 1)]
	for (y, x) in yxs:
		acc[y + 1][x + 1] += 1
	for j in range(l):
		for i in range(l):
			acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]
	max_cnt = 0
	for j in range(k):
		for i in range(k):
			cnt = count_up(0, 0, j - 1, i - 1) + count_up(0, i + k, j - 1, l - 1) + count_up(j, i, j + k - 1, i + k - 1) + count_up(j + k, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
			max_cnt = max(max_cnt, max(n - cnt, cnt))
	print(max_cnt)
main()


(n, k) = map(int, input().split())
g = [[[0, 0] for i in range(k)] for j in range(k)]
for i in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if y % (2 * k) < k and x % (2 * k) < k:
		g[y % (2 * k)][x % (2 * k)][c == 'W'] += 1
	elif y % (2 * k) < k:
		g[y % (2 * k)][x % (2 * k) - k][c == 'B'] += 1
	elif x % (2 * k) < k:
		g[y % (2 * k) - k][x % (2 * k)][c == 'B'] += 1
	else:
		g[y % (2 * k) - k][x % (2 * k) - k][c == 'W'] += 1
ac = [[[0] * (k + 1) for i in range(k + 1)] for l in range(2)]
for l in range(2):
	for i in range(k):
		for j in range(k):
			ac[l][i + 1][j + 1] = ac[l][i][j + 1] + ac[l][i + 1][j] - ac[l][i][j] + g[i][j][l]
ans = 0
for i in range(k):
	for j in range(k):
		ans = max(ac[0][k][k] - ac[0][i][k] - ac[0][k][j] + 2 * ac[0][i][j] + ac[1][i][k] + ac[1][k][j] - 2 * ac[1][i][j], ans)
		ans = max(ac[1][k][k] - ac[1][i][k] - ac[1][k][j] + 2 * ac[1][i][j] + ac[0][i][k] + ac[0][k][j] - 2 * ac[0][i][j], ans)
print(ans)


(N, K) = map(int, input().split())
dp = [[0] * (3 * K + 1) for _ in range(2 * K + 1)]
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		x += K
	m = min(x // K, y // K)
	x -= m * K
	y -= m * K
	x %= 2 * K
	y %= 2 * K
	dp[x][y] += 1
	if x < K and y < K:
		dp[x + K][y + K] += 1
		dp[x][y + 2 * K] += 1
	elif x < K and y >= K:
		dp[x + K][y - K] += 1
		dp[x + K][y + K] += 1
	elif x >= K and y < K:
		dp[x - K][y + K] += 1
		dp[x][y + 2 * K] += 1
for i in range(2 * K):
	for j in range(3 * K):
		dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
ret = 0
for i in range(K - 1, 2 * K):
	for j in range(K - 1, 3 * K):
		ret = max(ret, dp[i][j] - dp[i][j - K] - dp[i - K][j] + dp[i - K][j - K])
print(ret)


import sys
import numpy as np
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(N, K) = lr()
table = [[0] * 2 * K for _ in range(K)]
for _ in range(N):
	(x, y, c) = sr().split()
	x = int(x)
	y = int(y)
	if c == 'B':
		x -= K
	x %= 2 * K
	y %= 2 * K
	if x >= K and y >= K:
		x -= K
		y -= K
	elif y >= K:
		x += K
		y -= K
	table[y][x] += 1
table = np.array(table, dtype=np.int32)
table = np.concatenate([table, table], axis=1)
table2 = np.roll(table, K, axis=1)
table = np.concatenate([table, table2], axis=0)
table_cum = table.cumsum(axis=1).cumsum(axis=0)
prev = table_cum.copy()
table_cum[:, K:] -= prev[:, :-K]
table_cum[K:, :] -= prev[:-K, :]
table_cum[K:, K:] += prev[:-K, :-K]
answer = table_cum.max()
print(answer)


import sys
from pprint import *
(N, K) = map(int, input().split())
input = sys.stdin.readline
dpb = [[0 for _ in range(2 * K + 1)] for _ in range(2 * K + 1)]

def getmatnum(x1, y1, x2, y2, mat):
	if x2 == 0 or y2 == 0:
		return 0
	else:
		if x1 == -1:
			x1 = 0
		if y1 == -1:
			y1 = 0
		return mat[y2][x2] - mat[y1][x2] - mat[y2][x1] + mat[y1][x1]
for i in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'W':
		dpb[y % (2 * K) + 1][(x + K) % (2 * K) + 1] = dpb[y % (2 * K) + 1][(x + K) % (2 * K) + 1] + 1
	else:
		dpb[y % (2 * K) + 1][x % (2 * K) + 1] = dpb[y % (2 * K) + 1][x % (2 * K) + 1] + 1
for i in range(1, 2 * K + 1):
	for j in range(1, 2 * K + 1):
		dpb[i][j] = dpb[i][j - 1] + dpb[i][j]
for i in range(1, 2 * K + 1):
	for j in range(1, 2 * K + 1):
		dpb[i][j] = dpb[i - 1][j] + dpb[i][j]
ans = 0
for i in range(K):
	for j in range(K):
		k = i + K
		l = j + K
		b1 = getmatnum(-1, -1, i, j, dpb) + getmatnum(k, -1, 2 * K, j, dpb) + getmatnum(i, j, k, l, dpb) + getmatnum(-1, l, i, 2 * K, dpb) + getmatnum(k, l, 2 * K, 2 * K, dpb)
		b2 = getmatnum(i, -1, k, j, dpb) + getmatnum(-1, j, i, l, dpb) + getmatnum(k, j, 2 * K, l, dpb) + getmatnum(i, l, k, 2 * K, dpb)
		ans = max(ans, b1, b2)
print(ans)


import sys
sys.setrecursionlimit(10000)
INF = float('inf')
(N, K) = map(int, next(sys.stdin).split())
MOD = K * 2
grid = [[0 for _ in range(MOD)] for _ in range(MOD)]

def draw(sx, sy, ex, ey):
	sx %= MOD
	sy %= MOD
	ex = (ex - 1) % MOD + 1
	ey = (ey - 1) % MOD + 1
	if sx < ex and sy < ey:
		grid[sx][sy] += 1
		if ex < MOD:
			grid[ex][sy] -= 1
		if ey < MOD:
			grid[sx][ey] -= 1
		if ex < MOD and ey < MOD:
			grid[ex][ey] += 1
	elif ex <= sx:
		if ey <= sy:
			draw(0, 0, ex, ey)
			draw(0, sy, ex, MOD)
			draw(sx, 0, MOD, ey)
			draw(sx, sy, MOD, MOD)
		else:
			draw(0, sy, ex, ey)
			draw(sx, sy, MOD, ey)
	else:
		draw(sx, 0, ex, ey)
		draw(sx, sy, ex, MOD)
for _ in range(N):
	(x, y, c) = next(sys.stdin).split()
	x = int(x)
	y = int(y)
	if c == 'W':
		x += K
	draw(x, y, x + K, y + K)
	draw(x + K, y + K, x + K * 2, y + K * 2)
for i in range(MOD - 1):
	for j in range(MOD):
		grid[i + 1][j] += grid[i][j]
for i in range(MOD - 1):
	for j in range(MOD):
		grid[j][i + 1] += grid[j][i]
ans = 0
for i in range(MOD):
	ans = max(ans, *grid[i])
print(ans)


import sys
input = sys.stdin.readline

def main():
	(N, K) = map(int, input().split())
	L = 2 * K
	state = [[[0, 0] for _ in range(L + 1)] for _ in range(K + 1)]
	for _ in range(N):
		(x, y, c) = map(str, input().split())
		(a, b) = (int(x) % L, int(y) % L)
		k = 0 if c == 'W' else 1
		if a >= K:
			if b >= K:
				a -= K
				b -= K
			else:
				a -= K
				b += K
		state[a + 1][b + 1][k] += 1
	for i in range(K):
		for j in range(L):
			for k in [0, 1]:
				state[i + 1][j + 1][k] += state[i + 1][j][k] + state[i][j + 1][k] - state[i][j][k]
	ans = 0
	for x in range(K):
		for y in range(K):
			for k in [0, 1]:
				count = 0
				count += state[x][y][k]
				count += state[x][L][k] - state[x][y + K][k]
				count += state[K][y + K][k] - state[x][y + K][k] - state[K][y][k] + state[x][y][k]
				l = k ^ 1
				count += state[K][y][l] - state[x][y][l]
				count += state[x][y + K][l] - state[x][y][l]
				count += state[K][L][l] - state[K][y + K][l] - state[x][L][l] + state[x][y + K][l]
				ans = max(ans, count)
	print(ans)
main()


def main():
	(N, K) = map(int, input().split())
	xy = []
	for _ in range(N):
		(x, y, c) = input().split()
		a = K if c == 'B' else 0
		xy.append((int(x), int(y) + a))
	AC(K, xy)

def fix(x, K2):
	return min(max(0, x), K2)

def boundary(x, y, K):
	K2 = K * 2
	s = set()
	for i in range(-2, 2):
		for j in range(-2, 2):
			if (i + j) % 2 == 1:
				continue
			x1 = fix(x + i * K, K2)
			y1 = fix(y + j * K, K2)
			x2 = fix(x + (i + 1) * K, K2)
			y2 = fix(y + (j + 1) * K, K2)
			if x1 == x2 or y1 == y2:
				continue
			s.add(((x1, y1), (x2, y2)))
	return s

def AC(K, xy):
	K2 = K * 2
	w = [[0] * (K2 + 1) for _ in range(K2 + 1)]
	for (x, y) in xy:
		x = x % K2
		y = y % K2
		for ((x1, y1), (x2, y2)) in boundary(x, y, K):
			w[y1][x1] += 1
			w[y1][x2] -= 1
			w[y2][x1] -= 1
			w[y2][x2] += 1
	ans = max_cumsum(w)
	print(ans)

def max_cumsum(w):
	import copy
	w = copy.deepcopy(w)
	l = len(w)
	for i in range(1, l):
		for j in range(l):
			w[j][i] += w[j][i - 1]
	for i in range(l):
		for j in range(1, l):
			w[j][i] += w[j - 1][i]
	return max(map(max, w))

def TLE(K, xy):
	ans = 0
	for i in range(2 * K):
		for j in range(2 * K):
			tmp_ans = 0
			for (x, y) in xy:
				x = (x + i) % (2 * K)
				y = (y + j) % (2 * K)
				w1 = x < K and y < K
				w2 = x >= K and y >= K
				if w1 or w2:
					tmp_ans += 1
			ans = max(ans, tmp_ans)
	print(ans)
main()


import sys
input = sys.stdin.readline

def query(x1, y1, x2, y2, acc):
	return acc[x2 + 1][y2 + 1] - acc[x2 + 1][y1] - acc[x1][y2 + 1] + acc[x1][y1]
(N, K) = map(int, input().split())
W = [[0] * K for _ in range(K)]
B = [[0] * K for _ in range(K)]
for _ in range(N):
	(xi, yi, ci) = input().split()
	xi = int(xi)
	yi = int(yi)
	t = (xi - xi % K) // K + (yi - yi % K) // K
	if t % 2 == 1:
		ci = 'B' if ci == 'W' else 'W'
	if ci == 'W':
		W[xi % K][yi % K] += 1
	else:
		B[xi % K][yi % K] += 1
W_acc = [[0] * (K + 1) for _ in range(K + 1)]
B_acc = [[0] * (K + 1) for _ in range(K + 1)]
for i in range(K):
	for j in range(K):
		W_acc[i + 1][j + 1] = W_acc[i + 1][j] + W_acc[i][j + 1] - W_acc[i][j] + W[i][j]
		B_acc[i + 1][j + 1] = B_acc[i + 1][j] + B_acc[i][j + 1] - B_acc[i][j] + B[i][j]
ans = 0
for i in range(K):
	for j in range(K):
		clear_w = query(0, 0, i, j, W_acc) + query(i + 1, j + 1, K - 1, K - 1, W_acc)
		clear_b = query(i + 1, 0, K - 1, j, B_acc) + query(0, j + 1, i, K - 1, B_acc)
		ans = max(ans, clear_w + clear_b)
		clear_w = query(0, 0, i, j, B_acc) + query(i + 1, j + 1, K - 1, K - 1, B_acc)
		clear_b = query(i + 1, 0, K - 1, j, W_acc) + query(0, j + 1, i, K - 1, W_acc)
		ans = max(ans, clear_w + clear_b)
print(ans)


(n, k) = map(int, input().split())
xyc_ = [list(input().split()) for _ in range(n)]
xyc = []
for (x, y, c) in xyc_:
	x = int(x) % (2 * k)
	y = int(y) % (2 * k)
	if x >= k and y >= k:
		x %= k
		y %= k
	elif x >= k:
		x %= k
		c = 'W' if c == 'B' else 'B'
	elif y >= k:
		y %= k
		c = 'W' if c == 'B' else 'B'
	xyc.append([x, y, c])
wmap = [[0] * k for _ in range(k)]
bmap = [[0] * k for _ in range(k)]
for (x, y, c) in xyc:
	if c == 'B':
		bmap[x][y] += 1
	else:
		wmap[x][y] += 1
for i in range(k):
	for j in range(k - 1):
		bmap[i][j + 1] += bmap[i][j]
		wmap[i][j + 1] += wmap[i][j]
for i in range(k):
	for j in range(k - 1):
		bmap[j + 1][i] += bmap[j][i]
		wmap[j + 1][i] += wmap[j][i]

def func(x, y):
	(bret, wret) = (0, 0)
	bret = bmap[-1][-1]
	wret = wmap[-1][-1]
	if x == 0 and y == 0:
		pass
	elif x == 0:
		bret -= bmap[-1][y - 1]
		wret -= wmap[-1][y - 1]
	elif y == 0:
		bret -= bmap[x - 1][-1]
		wret -= wmap[x - 1][-1]
	else:
		bret -= bmap[-1][y - 1]
		wret -= wmap[-1][y - 1]
		bret -= bmap[x - 1][-1]
		wret -= wmap[x - 1][-1]
		bret += 2 * bmap[x - 1][y - 1]
		wret += 2 * wmap[x - 1][y - 1]
	return max(wmap[-1][-1] - wret + bret, bmap[-1][-1] - bret + wret)
ans = 0
for i in range(k):
	for j in range(k):
		ans = max(ans, func(i, j))
print(ans)


import numpy as np
(n, k) = map(int, input().split())
list_subj = []
for i in range(n):
	(x, y, c) = input().split()
	if c == 'W':
		list_subj.append([int(x) % (2 * k), int(y) % (2 * k)])
	else:
		list_subj.append([(int(x) + k) % (2 * k), int(y) % (2 * k)])
imos_field = [[0] * (2 * k + 1) for _ in range(2 * k + 1)]
for (x, y) in list_subj:
	for place in (0, k):
		adj_y = (y + place) % (2 * k)
		if adj_y >= k - 1:
			imos_y = [(adj_y - k + 1, adj_y + 1)]
		else:
			imos_y = [(0, adj_y + 1), (adj_y + k + 1, 2 * k)]
		adj_x = (x + place) % (2 * k)
		if adj_x >= k - 1:
			imos_x = [(adj_x - k + 1, adj_x + 1)]
		else:
			imos_x = [(0, adj_x + 1), (adj_x + k + 1, 2 * k)]
		for (bottom, top) in imos_y:
			for (left, right) in imos_x:
				imos_field[bottom][left] += 1
				imos_field[top][left] -= 1
				imos_field[bottom][right] -= 1
				imos_field[top][right] += 1
result = np.cumsum(imos_field, axis=1)
result = np.cumsum(result, axis=0)
result = int(np.max(result))
print(result)


def main():
	(n, k) = map(int, input().split())
	e = [[0] * (2 * k) for i in range(2 * k)]
	mod = 2 * k
	for i in range(n):
		(x, y, c) = input().split()
		if c == 'W':
			x = int(x) % mod
			y = (int(y) + k) % mod
		else:
			x = int(x) % mod
			y = int(y) % mod
		e[x][y] += 1
	for i in range(mod):
		for j in range(mod):
			if i != 0 and j != 0:
				e[i][j] -= e[i - 1][j - 1]
			if i != 0:
				e[i][j] += e[i - 1][j]
			if j != 0:
				e[i][j] += e[i][j - 1]
	ans = 0
	for i in range(k + 1):
		for j in range(k + 1):
			count = e[-1][-1] + 2 * e[i + k - 1][j + k - 1] - e[-1][j + k - 1] - e[i + k - 1][-1]
			if i != 0 and j != 0:
				count += 2 * e[i - 1][j - 1]
			if i != 0:
				count += e[i - 1][-1] - 2 * e[i - 1][j + k - 1]
			if j != 0:
				count += e[-1][j - 1] - 2 * e[i + k - 1][j - 1]
			ans = max(ans, count, n - count)
	print(ans)
main()


(n, k) = map(int, input().split())
favor = [tuple(input().split()) for _ in range(n)]
square = [[0 for _ in range(k)] for _ in range(k)]
initscore = 0
for (xc, yc, c) in favor:
	x = int(xc)
	y = int(yc)
	x %= 2 * k
	y %= 2 * k
	if x in range(k) and y in range(k) or (x in range(k, 2 * k) and y in range(k, 2 * k)):
		if c == 'B':
			square[x % k][y % k] += 1
		else:
			square[x % k][y % k] -= 1
			initscore += 1
	elif c == 'B':
		square[x % k][y % k] -= 1
		initscore += 1
	else:
		square[x % k][y % k] += 1
cum = [[0 for _ in range(k + 1)] for _ in range(k + 1)]
for i in range(k):
	for j in range(k):
		cum[i + 1][j + 1] = cum[i + 1][j] + cum[i][j + 1] - cum[i][j] + square[i][j]
maxscore = 0
for i in range(k):
	for j in range(k):
		score = cum[i][k] + cum[k][j] - 2 * cum[i][j]
		maxscore = max(maxscore, max(cum[k][k] - score, score))
print(maxscore + initscore)


(n, k) = map(int, input().split())
v = []
for i in range(n):
	abc = [x for x in input().split()]
	a = int(abc[0])
	b = int(abc[1])
	c = abc[2]
	if c == 'W':
		b += k
	v.append((a % (2 * k), b % (2 * k)))
s = [[0 for _ in range(3 * k + 1)] for _ in range(3 * k + 1)]
s2 = [[0 for _ in range(k + 1)] for _ in range(k + 1)]
s3 = [[0 for _ in range(k + 1)] for _ in range(k + 1)]
for i in range(n):
	s[v[i][1]][v[i][0]] += 1
	s[v[i][1] + k][v[i][0] + k] += 1
	s[v[i][1] + k][v[i][0]] -= 1
	s[v[i][1]][v[i][0] + k] -= 1
for i in range(3 * k):
	for j in range(3 * k):
		s[i + 1][j] += s[i][j]
for i in range(3 * k):
	for j in range(3 * k):
		s[i][j + 1] += s[i][j]
for i in range(3 * k):
	for j in range(3 * k):
		c = (i // k + j // k) % 2
		if c == 0:
			s2[i % k][j % k] += s[i][j]
		else:
			s3[i % k][j % k] += s[i][j]
res = 0
for i in range(k):
	for j in range(k):
		res = max(res, s2[i][j])
		res = max(res, s3[i][j])
print(res)


(N, K) = map(int, input().split())
x = [0 for i in range(N)]
y = [0 for i in range(N)]
for i in range(N):
	(a, b, c) = map(str, input().split())
	(a, b) = (int(a), int(b))
	if c == 'W':
		b += K
	a %= 2 * K
	b %= 2 * K
	x[i] = a
	y[i] = b
board = [[0 for j in range(2 * K + 1)] for i in range(2 * K + 1)]
for i in range(N):
	if y[i] >= K:
		y[i] -= K
		x[i] = (x[i] + K) % (2 * K)
	if x[i] >= K and y[i] < K:
		board[0][y[i] + 1] += 1
		board[0][y[i] + K + 1] -= 1
		board[x[i] - K + 1][y[i] + 1] -= 2
		board[x[i] - K + 1][y[i] + K + 1] += 2
		board[x[i] - K + 1][0] += 1
		board[x[i] + 1][0] -= 1
		board[x[i] + 1][y[i] + 1] += 2
		board[x[i] + 1][y[i] + K + 1] -= 2
	if x[i] < K and y[i] < K:
		board[0][y[i] + 1] -= 1
		board[0][y[i] + K + 1] += 1
		board[x[i] + K + 1][y[i] + 1] -= 2
		board[x[i] + K + 1][y[i] + K + 1] += 2
		board[x[i] + K + 1][0] += 1
		board[x[i] + 1][0] -= 1
		board[x[i] + 1][y[i] + 1] += 2
		board[x[i] + 1][y[i] + K + 1] -= 2
		board[0][0] += 1
coard = [[0 for j in range(2 * K)] for i in range(2 * K)]
doard = [[0 for j in range(2 * K)] for i in range(2 * K)]
for i in range(2 * K):
	for j in range(2 * K):
		if i == 0:
			coard[i][j] = board[i][j]
		else:
			coard[i][j] = board[i][j] + coard[i - 1][j]
for i in range(2 * K):
	for j in range(2 * K):
		if j == 0:
			doard[i][j] = coard[i][j]
		else:
			doard[i][j] = coard[i][j] + doard[i][j - 1]
ans = 0
for i in range(2 * K):
	for j in range(2 * K):
		ans = max(ans, doard[i][j])
print(ans)


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
MX = 2 * K
A = [[0] * MX for _ in range(MX)]
for i in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += K
	A[y % MX][x % MX] += 1
S = [[0] * (MX + 1) for _ in range(MX + 1)]
for y in range(MX):
	for x in range(MX):
		S[y + 1][x + 1] = S[y + 1][x] + S[y][x + 1] - S[y][x] + A[y][x]
ans = 0
for y in range(K, MX + 1):
	for x in range(K, MX + 1):
		s = S[y][x] - S[y - K][x] - S[y][x - K] + S[y - K][x - K]
		s += S[y - K][x - K]
		s += S[MX][x - K] - S[y][x - K]
		s += S[y - K][MX] - S[y - K][x]
		s += S[MX][MX] - S[y][MX] - S[MX][x] + S[y][x]
		ans = max(ans, s, N - s)
print(ans)


def calcb(lx, ly, rx, ry):
	if rx < 0 or ry < 0 or lx > rx or (ly > ry):
		return 0
	ret = acum1[ry * 2 * k + rx]
	if lx != 0:
		ret -= acum1[ry * 2 * k + lx - 1]
	if ly != 0:
		ret -= acum1[(ly - 1) * 2 * k + rx]
	if lx != 0 and ly != 0:
		ret += acum1[(ly - 1) * 2 * k + lx - 1]
	return ret

def calcw(lx, ly, rx, ry):
	if rx < 0 or ry < 0 or lx > rx or (ly > ry):
		return 0
	ret = acum2[ry * 2 * k + rx]
	if lx != 0:
		ret -= acum2[ry * 2 * k + lx - 1]
	if ly != 0:
		ret -= acum2[(ly - 1) * 2 * k + rx]
	if lx != 0 and ly != 0:
		ret += acum2[(ly - 1) * 2 * k + lx - 1]
	return ret
(n, k) = map(int, input().split())
board1 = [0] * (4 * k * k)
board2 = [0] * (4 * k * k)
acum1 = [0] * (4 * k * k)
acum2 = [0] * (4 * k * k)
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x) % (2 * k), int(y) % (2 * k))
	if c == 'B':
		board1[y * 2 * k + x] += 1
	if c == 'W':
		board2[y * 2 * k + x] += 1
for i in range(2 * k):
	for j in range(2 * k):
		acum1[i * 2 * k + j] = board1[i * 2 * k + j]
		acum2[i * 2 * k + j] = board2[i * 2 * k + j]
		if i != 0:
			acum1[i * 2 * k + j] += acum1[(i - 1) * 2 * k + j]
			acum2[i * 2 * k + j] += acum2[(i - 1) * 2 * k + j]
		if j != 0:
			acum1[i * 2 * k + j] += acum1[i * 2 * k + j - 1]
			acum2[i * 2 * k + j] += acum2[i * 2 * k + j - 1]
		if i != 0 and j != 0:
			acum1[i * 2 * k + j] -= acum1[(i - 1) * 2 * k + j - 1]
			acum2[i * 2 * k + j] -= acum2[(i - 1) * 2 * k + j - 1]
ans = 0
for i in range(k):
	for j in range(k):
		tmp1 = 0
		tmp2 = 0
		(lx, ly) = (i, j)
		(rx, ry) = (lx + k - 1, ly + k - 1)
		tmp1 += calcb(lx, ly, rx, ry)
		tmp2 += calcw(lx, ly, rx, ry)
		(tlx, tly) = (0, 0)
		(trx, ttry) = (lx - 1, ly - 1)
		tmp1 += calcb(tlx, tly, trx, ttry)
		tmp2 += calcw(tlx, tly, trx, ttry)
		(tlx, tly) = (rx + 1, 0)
		(trx, ttry) = (2 * k - 1, ly - 1)
		tmp1 += calcb(tlx, tly, trx, ttry)
		tmp2 += calcw(tlx, tly, trx, ttry)
		(tlx, tly) = (0, ry + 1)
		(trx, ttry) = (lx - 1, 2 * k - 1)
		tmp1 += calcb(tlx, tly, trx, ttry)
		tmp2 += calcw(tlx, tly, trx, ttry)
		(tlx, tly) = (rx + 1, ry + 1)
		(trx, ttry) = (2 * k - 1, 2 * k - 1)
		tmp1 += calcb(tlx, tly, trx, ttry)
		tmp2 += calcw(tlx, tly, trx, ttry)
		(tlx, tly) = (lx, 0)
		(trx, ttry) = (rx, ly - 1)
		tmp1 += calcw(tlx, tly, trx, ttry)
		tmp2 += calcb(tlx, tly, trx, ttry)
		(tlx, tly) = (0, ly)
		(trx, ttry) = (lx - 1, ry)
		tmp1 += calcw(tlx, tly, trx, ttry)
		tmp2 += calcb(tlx, tly, trx, ttry)
		(tlx, tly) = (rx + 1, ly)
		(trx, ttry) = (2 * k - 1, ry)
		tmp1 += calcw(tlx, tly, trx, ttry)
		tmp2 += calcb(tlx, tly, trx, ttry)
		(tlx, tly) = (lx, ry + 1)
		(trx, ttry) = (rx, 2 * k - 1)
		tmp1 += calcw(tlx, tly, trx, ttry)
		tmp2 += calcb(tlx, tly, trx, ttry)
		ans = max(ans, tmp1, tmp2)
print(ans)


import sys
input = sys.stdin.readline
INF = float('inf')

class Acc2D:

	def __init__(self, grid):
		(self.h, self.w) = (len(grid), len(grid[0]))
		self.acc2D = self._build(self.h, self.w, grid)

	def _build(self, h, w, grid):
		ret = [[0] * (w + 1) for _ in range(h + 1)]
		for r in range(h):
			for c in range(w):
				ret[r + 1][c + 1] = ret[r][c + 1] + ret[r + 1][c] - ret[r][c] + grid[r][c]
		return ret

	def get(self, r1, r2, c1, c2):
		if r1 < 0:
			r1 = 0
		if r2 >= self.h:
			r2 = self.h
		if c1 < 0:
			c1 = 0
		if c2 >= self.w:
			c2 = self.w
		cnt = self.acc2D
		return cnt[r2][c2] - cnt[r1][c2] - cnt[r2][c1] + cnt[r1][c1]

def main():
	(n, k) = map(int, input().split())
	g = [[0] * (2 * k) for _ in range(2 * k)]
	for _ in range(n):
		(x, y, c) = input().split()
		x = int(x) % (2 * k)
		y = int(y) % (2 * k)
		if c == 'B':
			y = (y + k) % (2 * k)
		g[y][x] += 1
	acc2d = Acc2D(g)
	ans = 0
	for r in range(k):
		for c in range(k):
			tmp = acc2d.get(0, r, 0, c)
			tmp += acc2d.get(0, r, c + k, c + k * 2)
			tmp += acc2d.get(r, r + k, c, c + k)
			tmp += acc2d.get(r + k, r + k * 2, 0, c)
			tmp += acc2d.get(r + k, r + k * 2, c + k, c + k * 2)
			ans = max(ans, tmp, n - tmp)
	print(ans)
main()


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
grid = [[0] * k for i in range(k)]
black = 0
for i in range(n):
	(a, b, c) = input().split()
	(a, b) = (int(a), int(b))
	f = a // k + b // k
	a %= k
	b %= k
	if c == 'W' and f % 2 == 0 or (c == 'B' and f % 2 == 1):
		grid[a][b] += 1
	else:
		grid[a][b] -= 1
		black += 1
s = [[0] * (k + 1) for i in range(k + 1)]
for i in range(k):
	for j in range(k):
		s[i + 1][j + 1] = s[i][j + 1] + s[i + 1][j] - s[i][j] + grid[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		a = s[k][k] - s[i][k] - s[k][j] + 2 * s[i][j] + black
		ans = max(ans, max(a, n - a))
print(ans)


(N, K) = map(int, input().split())
B = [[0] * (2 * K) for _ in range(2 * K)]
for _ in range(N):
	(x, y, c) = input().split()
	if c == 'B':
		B[int(x) % (2 * K)][int(y) % (2 * K)] += 1
	else:
		B[int(x) % (2 * K)][(int(y) + K) % (2 * K)] += 1
SB = [[0] * (4 * K + 1) for _ in range(4 * K + 1)]
for x in range(4 * K):
	for y in range(4 * K):
		SB[x][y] = B[x % (2 * K)][y % (2 * K)]
for x in range(4 * K):
	for y in range(4 * K):
		SB[x + 1][y] += SB[x][y]
for y in range(4 * K):
	for x in range(4 * K):
		SB[x][y + 1] += SB[x][y]
ans = 0
for x in range(2 * K):
	for y in range(2 * K):
		cand = SB[x + K][y + K] - SB[x][y + K] - SB[x + K][y] + SB[x][y]
		cand += SB[x + 2 * K][y + 2 * K] - SB[x + K][y + 2 * K] - SB[x + 2 * K][y + K] + SB[x + K][y + K]
		ans = max(ans, cand)
print(ans)


import sys
input = sys.stdin.readline

def cumsum_2d(inlist, h, w):
	outlist = [[inlist[i - 1][j - 1] if i != 0 and j != 0 else 0 for i in range(k + 1)] for j in range(k + 1)]
	for i in range(1, h + 1):
		tmp = 0
		for j in range(1, w + 1):
			tmp += outlist[i][j]
			outlist[i][j] = tmp
	for i in range(1, w + 1):
		tmp = 0
		for j in range(1, h + 1):
			tmp += outlist[j][i]
			outlist[j][i] = tmp
	return outlist
(n, k) = [int(v) for v in input().split()]
hope = [[a for a in input().split()] for i in range(n)]
b_board = [[0 for i in range(k)] for j in range(k)]
w_board = [[0 for i in range(k)] for j in range(k)]
for (h, w, c) in hope:
	(a1, a2) = divmod(int(w), k)
	(b1, b2) = divmod(int(h), k)
	if (a1 + b1) % 2 == 1:
		if c == 'B':
			c = 'W'
		else:
			c = 'B'
	if c == 'B':
		b_board[b2][a2] += 1
	else:
		w_board[b2][a2] += 1
b_board_3 = cumsum_2d(b_board, k, k)
w_board_3 = cumsum_2d(w_board, k, k)
(total_b, total_w) = (b_board_3[k][k], w_board_3[k][k])
ans = 0
for i in range(1, k + 1):
	for j in range(1, k + 1):
		b1 = b_board_3[i][j] - b_board_3[0][j] - b_board_3[i][0]
		b2 = total_b - b_board_3[i][k] - b_board_3[k][j] + b_board_3[i][j]
		w1 = w_board_3[i][j] - w_board_3[0][j] - w_board_3[i][0]
		w2 = total_w - w_board_3[i][k] - w_board_3[k][j] + w_board_3[i][j]
		ans = max(b1 + b2 + (total_w - w1 - w2), w1 + w2 + (total_b - b1 - b2), ans)
print(ans)


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
white = [[0] * (K + 1) for _ in range(2 * K + 1)]
black = [[0] * (K + 1) for _ in range(2 * K + 1)]
for _ in range(N):
	(str_h, str_w, c) = map(str, input().rstrip().split())
	h = int(str_h) % (2 * K)
	w = int(str_w) % (2 * K)
	if c == 'W':
		if w >= K:
			if h >= K:
				white[h - K + 1][w - K + 1] += 1
			else:
				white[h + K + 1][w - K + 1] += 1
		else:
			white[h + 1][w + 1] += 1
	elif w >= K:
		if h >= K:
			black[h - K + 1][w - K + 1] += 1
		else:
			black[h + K + 1][w - K + 1] += 1
	else:
		black[h + 1][w + 1] += 1
for h in range(1, 2 * K + 1):
	for w in range(1, K + 1):
		white[h][w] += white[h][w - 1] + white[h - 1][w] - white[h - 1][w - 1]
		black[h][w] += black[h][w - 1] + black[h - 1][w] - black[h - 1][w - 1]
ans = 0
for h in range(K + 1):
	for w in range(K + 1):
		b1 = black[h][w]
		b2 = black[h + K][K] - black[h + K][w] - black[h][K] + black[h][w]
		b3 = black[2 * K][w] - black[h + K][w]
		w1 = white[h][K] - white[h][w]
		w2 = white[h + K][w] - white[h][w]
		w3 = white[2 * K][K] - white[2 * K][w] - white[h + K][K] + white[h + K][w]
		ans = max(ans, b1 + b2 + b3 + w1 + w2 + w3)
		b1 = white[h][w]
		b2 = white[h + K][K] - white[h + K][w] - white[h][K] + white[h][w]
		b3 = white[2 * K][w] - white[h + K][w]
		w1 = black[h][K] - black[h][w]
		w2 = black[h + K][w] - black[h][w]
		w3 = black[2 * K][K] - black[2 * K][w] - black[h + K][K] + black[h + K][w]
		ans = max(ans, b1 + b2 + b3 + w1 + w2 + w3)
print(ans)


import numpy as np
(N, K) = map(int, input().split())
mod = 2 * K
field = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]

def gen_pattern(x):
	if x < K - 1:
		return [[0, x + 1], [x + K + 1, 2 * K]]
	else:
		return [[x - K + 1, x + 1]]
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += K
	x %= mod
	y %= mod
	for tmp in [0, K]:
		for (l, r) in gen_pattern((x + tmp) % mod):
			for (b, t) in gen_pattern((y + tmp) % mod):
				field[l][b] += 1
				field[l][t] -= 1
				field[r][b] -= 1
				field[r][t] += 1
print(np.max(np.cumsum(np.cumsum(field, axis=0), axis=1)))


(n, k) = map(int, input().split())
B = [[0] * k for i in range(k)]
W = [[0] * k for i in range(k)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	x %= 2 * k
	y %= 2 * k
	if (k <= x) == (k <= y):
		(B if c == 'B' else W)[y % k][x % k] += 1
	else:
		(W if c == 'B' else B)[y % k][x % k] += 1

def calc(M, n):
	D = [[0] * (n + 1) for i in range(n + 1)]
	for i in range(1, n + 1):
		tmp = 0
		for j in range(1, n + 1):
			tmp += M[i - 1][j - 1]
			D[i][j] = tmp + D[i - 1][j]
	return D

def su(D, a, b, c, d):
	return D[d][c] - D[d][a] - D[b][c] + D[b][a]
DB = calc(B, k)
DW = calc(W, k)
ans = 0
for i in range(k):
	for j in range(k):
		ans = max(ans, su(DB, 0, 0, i, j) + su(DB, i, j, k, k) + su(DW, i, 0, k, j) + su(DW, 0, j, i, k))
		ans = max(ans, su(DW, 0, 0, i, j) + su(DW, i, j, k, k) + su(DB, i, 0, k, j) + su(DB, 0, j, i, k))
print(ans)


(N, K) = map(int, input().split())
Map = [[[0] * K for i in range(K)] for j in range(2)]
for i in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'B':
		c = 0
	else:
		c = 1
	(nx, ny) = (x // K, y // K)
	if (nx + ny) % 2:
		c = 1 - c
	x %= K
	y %= K
	Map[c][x][y] += 1

def get_v(c, x, y):
	global Map
	if x < 0 or y < 0:
		return 0
	else:
		return Map[c][x][y]
for c in range(2):
	for x in range(K):
		for y in range(K):
			Map[c][x][y] += get_v(c, x - 1, y) + get_v(c, x, y - 1) - get_v(c, x - 1, y - 1)
ans = 0
for x in range(K + 1):
	for y in range(K + 1):
		b2 = get_v(0, x - 1, K - 1) + get_v(0, K - 1, y - 1) - 2 * get_v(0, x - 1, y - 1)
		w1 = get_v(1, x - 1, K - 1) + get_v(1, K - 1, y - 1) - 2 * get_v(1, x - 1, y - 1)
		b1 = Map[0][K - 1][K - 1] - b2
		w2 = Map[1][K - 1][K - 1] - w1
		ans = max(ans, b1 + w1, b2 + w2)
print(ans)


from numpy import *
(N, K) = map(int, input().split())
m = zeros((3 * K, 3 * K), dtype=int)
L = 2 * K
o = 0
for _ in [0] * N:
	(x, y, c) = input().split()
	(x, y) = (int(x) % L, int(y) % L)
	t = c == 'W'
	m[x, y] += 2 * t - 1
	o += t
for _ in [0, 0]:
	m[L:] = m[:K]
	m = cumsum(m, axis=0)
	m[:L] -= m[K:]
	m = m.T
m = m[:L, :L]
print(o - int((m + roll(roll(m, K, axis=0), K, axis=1)).min()))


def main():
	(N, K) = map(int, input().split())
	white = [[0] * K for _ in range(2 * K)]
	for i in range(N):
		(x, y, c) = input().split()
		(x, y) = (int(x) % (2 * K), int(y) % (2 * K))
		if y >= K:
			y -= K
			x = (x - K) % (2 * K)
		if c == 'W':
			white[x][y] += 1
		else:
			white[(x + K) % (2 * K)][y] += 1
	ysum = [[0] * (2 * K) for _ in range(2 * K)]
	for i in range(2 * K):
		ysum[i][0] = sum(white[i][:K])
		for j in range(1, K):
			ysum[i][j] = ysum[i][j - 1] - white[i][j - 1]
		for j in range(K + 1, 2 * K):
			ysum[i][j] = ysum[i][j - 1] + white[i][(j + K - 1) % (2 * K)]
	csum = [[0] * (2 * K) for _ in range(2 * K)]
	ans = 0
	for i in range(2 * K):
		csum[0][i] = sum([ysum[x][i] for x in range(K)]) + sum([ysum[x][(i + K) % (2 * K)] for x in range(K, 2 * K)])
		for j in range(1, 2 * K):
			csum[j][i] = csum[j - 1][i] + ysum[(j + K - 1) % (2 * K)][i] - ysum[(j + K - 1) % (2 * K)][(i + K) % (2 * K)] - ysum[j - 1][i] + ysum[j - 1][(i + K) % (2 * K)]
	for i in range(2 * K):
		for j in range(K):
			ans = max(ans, csum[i][j])
	print(ans)
main()


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
dp = [[0] * k for i in range(k)]
for i in range(n):
	(x, y, z) = input().split()
	(x, y) = (int(x), int(y))
	if x // k % 2 == y // k % 2:
		if z == 'W':
			(x, y) = (x % k, y % k)
			dp[0][0] += 1
			if y < k - 1:
				dp[0][y + 1] -= 1
			if x < k - 1:
				dp[x + 1][0] -= 1
			if x < k - 1 and y < k - 1:
				dp[x + 1][y + 1] += 2
		else:
			(x, y) = (x % k, y % k)
			if y < k - 1:
				dp[0][y + 1] += 1
			if x < k - 1:
				dp[x + 1][0] += 1
			if x < k - 1 and y < k - 1:
				dp[x + 1][y + 1] -= 2
	elif z == 'W':
		(x, y) = (x % k, y % k)
		if y < k - 1:
			dp[0][y + 1] += 1
		if x < k - 1:
			dp[x + 1][0] += 1
		if x < k - 1 and y < k - 1:
			dp[x + 1][y + 1] -= 2
	else:
		(x, y) = (x % k, y % k)
		dp[0][0] += 1
		if y < k - 1:
			dp[0][y + 1] -= 1
		if x < k - 1:
			dp[x + 1][0] -= 1
		if x < k - 1 and y < k - 1:
			dp[x + 1][y + 1] += 2
for i in range(k):
	for j in range(1, k):
		dp[i][j] += dp[i][j - 1]
Max = 0
Min = n
for j in range(k):
	for i in range(k):
		if i == 0:
			Max = max(Max, dp[i][j])
			Min = min(Min, dp[i][j])
		else:
			dp[i][j] += dp[i - 1][j]
			Max = max(Max, dp[i][j])
			Min = min(Min, dp[i][j])
ans = max(Max, n - Min)
print(ans)


import sys

def MI():
	return map(int, sys.stdin.readline().rstrip().split())

def S():
	return sys.stdin.readline().rstrip()
(N, K) = MI()
black = [[0] * K for _ in range(K)]
white = [[0] * K for _ in range(K)]
for _ in range(N):
	(x, y, c) = map(str, S().split())
	(x, y) = (int(x), int(y))
	if (x // K + y // K) % 2 == 0 and c == 'B' or ((x // K + y // K) % 2 == 1 and c == 'W'):
		black[x % K][y % K] += 1
	else:
		white[x % K][y % K] += 1
black_accumulate = [[0] * (K + 1)] + [[0] + black[i] for i in range(K)]
white_accumulate = [[0] * (K + 1)] + [[0] + white[i] for i in range(K)]
for i in range(1, K + 1):
	for j in range(1, K + 1):
		black_accumulate[i][j] += black_accumulate[i][j - 1]
		white_accumulate[i][j] += white_accumulate[i][j - 1]
for i in range(1, K + 1):
	for j in range(1, K + 1):
		black_accumulate[i][j] += black_accumulate[i - 1][j]
		white_accumulate[i][j] += white_accumulate[i - 1][j]

def f(s, t, u, v):
	return black_accumulate[t][v] - black_accumulate[t][u] - black_accumulate[s][v] + black_accumulate[s][u]

def g(s, t, u, v):
	return white_accumulate[t][v] - white_accumulate[t][u] - white_accumulate[s][v] + white_accumulate[s][u]
ans = 0
for i in range(1, K + 1):
	for j in range(1, K + 1):
		ans = max(ans, f(0, i, 0, j) + f(i, K, j, K) + g(0, i, j, K) + g(i, K, 0, j))
		ans = max(ans, g(0, i, 0, j) + g(i, K, j, K) + f(0, i, j, K) + f(i, K, 0, j))
print(ans)


class Acc2D:

	def __init__(self, a):
		(h, w) = (len(a), len(a[0]))
		self.acc2D = self._build(h, w, a)

	def _build(self, h, w, a):
		ret = [[0] * (w + 1) for _ in range(h + 1)]
		for r in range(h):
			for c in range(w):
				ret[r + 1][c + 1] = ret[r][c + 1] + ret[r + 1][c] - ret[r][c] + a[r][c]
		return ret

	def get(self, r1, r2, c1, c2):
		acc2D = self.acc2D
		return acc2D[r2][c2] - acc2D[r1][c2] - acc2D[r2][c1] + acc2D[r1][c1]

def main():
	import sys
	input = sys.stdin.readline
	(n, k) = map(int, input().split())
	g = [[0] * (k * 2) for _ in range(k * 2)]
	for _ in range(n):
		(x, y, c_) = input().split()
		x = int(x) % (k * 2)
		y = int(y) % (k * 2)
		if c_ == 'W':
			y = (y + k) % (k * 2)
		g[y][x] += 1
	acc2D = Acc2D(g)
	ret = 0
	for r in range(k):
		for c in range(k):
			s = acc2D.get(0, r, 0, c) + acc2D.get(0, r, c + k, k * 2) + acc2D.get(r, r + k, c, c + k) + acc2D.get(r + k, k * 2, 0, c) + acc2D.get(r + k, k * 2, c + k, k * 2)
			ret = max(ret, s, n - s)
	print(ret)
main()


(N, K) = map(int, input().split())
ub = K * 2
b = [[0 for j in range(ub + 1)] for i in range(ub + 1)]

def create_bound(p):
	if p - K + 1 >= 0:
		return [(p - K + 1, p + 1)]
	else:
		return [(0, p + 1), ((p - K + 1) % ub, ub)]
for i in range(N):
	(x, y, c) = input().split()
	nx = int(x) % ub
	ny = (int(y) + (K if c == 'W' else 0)) % ub
	for add in [0, K]:
		for (xlb, xub) in create_bound((nx + add) % ub):
			for (ylb, yub) in create_bound((ny + add) % ub):
				b[xlb][ylb] += 1
				b[xlb][yub] -= 1
				b[xub][ylb] -= 1
				b[xub][yub] += 1
for i in range(ub):
	for j in range(ub):
		b[i][j + 1] += b[i][j]
for j in range(ub):
	for i in range(ub):
		b[i + 1][j] += b[i][j]
print(max((max(b[i]) for i in range(ub))))


(N, K) = map(int, input().split())
score = [[0] * (K + 1) for y in range(K + 1)]
base_hope = 0
for i in range(N):
	(x, y, c) = input().split()
	(x2, y2) = (int(x) // K, int(y) // K)
	(x_coord, y_coord) = (int(x) % K + 1, int(y) % K + 1)
	if (x2 + y2) % 2 == 0 and c == 'W' or ((x2 + y2) % 2 == 1 and c == 'B'):
		score[y_coord][x_coord] += 1
		base_hope += 1
	else:
		score[y_coord][x_coord] -= 1
xy = [(x, y) for x in range(K) for y in range(K)]
for (x, y) in xy:
	score[y + 1][x + 1] += score[y + 1][x] + score[y][x + 1] - score[y][x]
max_hope = 0
for (x, y) in xy:
	hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
	true_hope = hope if hope > N - hope else N - hope
	if true_hope > max_hope:
		max_hope = true_hope
print(max_hope)


(n, k) = map(int, input().split())
mod = 2 * k
imos = [[0] * (mod + 1) for i in range(mod + 1)]
board = [[0] * mod for _ in range(mod)]
delta = [(0, 0), (k, k), (k, -k), (-k, k), (-k, -k)]
m_cnt = 0
for i in range(n):
	(x, y, c) = input().split()
	(x, y) = map(int, [x, y])
	d = 1
	if c == 'W':
		x -= k
	(x, y) = (x % mod, y % mod)
	if x // k ^ y // k:
		x = (x + k) % mod
		d *= -1
		m_cnt += 1
	if x < k or y < k:
		x += k
		y += k
	imos[x + 1][y + 1] += d
	imos[x - k + 1][y - k + 1] += d
	imos[x + 1][y - k + 1] -= d
	imos[x - k + 1][y + 1] -= d
for i in range(1, mod):
	for j in range(mod):
		imos[i][j] += imos[i - 1][j]
ans = 0
for i in range(mod):
	for j in range(1, mod):
		imos[i][j] += imos[i][j - 1]
		for (di, dj) in delta:
			if 0 <= i + di < mod and 0 <= j + dj < mod:
				board[i + di][j + dj] += imos[i][j]
for i in range(mod):
	for j in range(mod):
		ans = max(ans, board[i][j])
print(ans + m_cnt)


(N, K) = map(int, input().split())
K2 = 2 * K
inmap = [[0] * K for _ in range(K2)]
accmap = [[0] * (K + 1) for _ in range(K2 + 1)]
inmapdeb = {}
for i in range(N):
	line = input().split()
	(x, y, wb) = (int(line[0]), int(line[1]), line[2])
	if wb == 'W':
		x += K
	y %= K2
	if y >= K:
		y = y % K
		x = (x + K) % K2
	else:
		x = x % K2
	inmap[x][y] += 1

def calc(x, y):
	return accmap[x][y] + (accmap[x + K][K] - accmap[x][K] - accmap[x + K][y] + accmap[x][y]) + (accmap[K2][y] - accmap[x + K][y])
for i in range(K2):
	for j in range(K):
		accmap[i + 1][j + 1] = inmap[i][j] + accmap[i + 1][j] + accmap[i][j + 1] - accmap[i][j]
res = 0
for i in range(K):
	for j in range(K):
		cand = calc(i, j)
		res = max(res, cand, N - cand)
print(res)


(n, k) = map(int, input().split())
g = [[0] * (k + 1) for _ in range(k + 1)]
(ans, b) = (0, 0)
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	d = x // k + y // k
	x %= k
	y %= k
	if (c == 'W') ^ d % 2:
		g[x + 1][y + 1] += 1
	else:
		g[x + 1][y + 1] -= 1
		b += 1
for i in range(k):
	for j in range(k):
		g[i + 1][j + 1] += g[i][j + 1] + g[i + 1][j] - g[i][j]
for i in range(k):
	for j in range(k):
		cur = g[k][k] - g[k][j] - g[i][k] + g[i][j] * 2 + b
		if cur > ans:
			ans = cur
		if n - cur > ans:
			ans = n - cur
print(ans)


import numpy as np
(N, K) = map(int, input().split())
K2 = K * 2
M = np.zeros((K2, K2), int)
nb = 0
for _ in range(N):
	(x, y, c) = input().split()
	x = int(x) % K2
	y = int(y) % K2
	if c == 'B':
		nb += 1
	M[x, y] += 1 if c == 'W' else -1
Sx = np.zeros((K2, K2 + 1), int)
for y in range(K2):
	Sx[:, y + 1] = Sx[:, y] + M[:, y]
S = np.zeros((K2 + 1, K2 + 1), int)
for x in range(K2):
	S[x + 1] = S[x] + Sx[x]
ans = -1000000
for x in range(K2):
	x0 = 0
	x1 = max(x - K, 0)
	x2 = x
	x3 = min(x + K, K2)
	x4 = K2
	T1 = S[x4] - S[x3] + S[x2] - S[x1]
	T2 = S[x3] - S[x2] + S[x1] - S[x0]
	U = T1[K2] - T1[K:K2] + T1[:K] + T2[K:K2] - T2[:K]
	ans = max(ans, np.max(U))
print(nb + ans)


import sys
fin = sys.stdin.readline
from collections import defaultdict
from pprint import pprint

def sum_range_2D(cuml_mat, coord_left_up, Kx, Ky):
	if Kx <= 0 or Ky <= 0:
		return 0
	(x, y) = coord_left_up
	if x - 1 >= 0 and y - 1 >= 0:
		left_up = cuml_mat[x - 1][y - 1]
	else:
		left_up = 0
	if x - 1 >= 0 and y + Ky - 1 >= 0:
		right_up = cuml_mat[x - 1][y + Ky - 1]
	else:
		right_up = 0
	if x + Kx - 1 >= 0 and y - 1 >= 0:
		left_down = cuml_mat[x + Kx - 1][y - 1]
	else:
		left_down = 0
	right_down = cuml_mat[min(len(cuml_mat) - 1, x + Kx - 1)][min(len(cuml_mat) - 1, y + Ky - 1)]
	return right_down - right_up - left_down + left_up
(N, K) = [int(elem) for elem in fin().split()]
black_desires = []
for _ in range(N):
	(x, y, c) = fin().split()
	x = int(x) % (2 * K)
	y = int(y) % (2 * K) if c == 'B' else (int(y) + K) % (2 * K)
	black_desires.append((x, y))
cuml_2D = [[0] * (2 * K) for _ in range(2 * K)]
for (x, y) in black_desires:
	cuml_2D[x][y] += 1
for i in range(2 * K):
	for j in range(1, 2 * K):
		cuml_2D[i][j] += cuml_2D[i][j - 1]
for j in range(2 * K):
	for i in range(1, 2 * K):
		cuml_2D[i][j] += cuml_2D[i - 1][j]
max_num = 0
for i in range(K + 1):
	for j in range(K):
		current_num = sum_range_2D(cuml_2D, (i, j), K, K) + sum_range_2D(cuml_2D, (0, 0), i, j) + sum_range_2D(cuml_2D, (0, j + K), i, K - j) + sum_range_2D(cuml_2D, (i + K, 0), K - i, j) + sum_range_2D(cuml_2D, (i + K, j + K), K - i, K - j)
		max_num = max(max_num, current_num)
for i in range(K + 1):
	for j in range(K, 2 * K):
		current_num = sum_range_2D(cuml_2D, (i, j), K, 2 * K - j) + sum_range_2D(cuml_2D, (0, j - K), i, K) + sum_range_2D(cuml_2D, (i + K, j - K), K - i, K) + sum_range_2D(cuml_2D, (i, 0), K, j - K)
		max_num = max(max_num, current_num)
print(max_num)


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def main():
	(n, k) = map(int, input().split())
	K = k * 2
	hope = [[0] * (K + 1) for _ in range(K + 1)]
	for _ in range(n):
		(x, y, c) = input().split()
		(x, y) = (int(x), int(y))
		if c == 'B':
			hope[1 + y % K][1 + x % K] += 1
		if c == 'W':
			hope[1 + (y + k) % K][1 + x % K] += 1
	sumB = [[0] * (K + 1) for _ in range(K + 1)]
	for y in range(1, K + 1):
		for x in range(1, K + 1):
			sumB[y][x] = sumB[y - 1][x] + sumB[y][x - 1] + hope[y][x] - sumB[y - 1][x - 1]

	def calc_sum(x0, y0, x1, y1):
		return sumB[y1][x1] - sumB[y0 - 1][x1] - sumB[y1][x0 - 1] + sumB[y0 - 1][x0 - 1]
	ans = 0
	for y in range(1, K + 1):
		for x in range(1, k + 1):
			if y <= k:
				val = calc_sum(1, 1, x - 1, y - 1) + calc_sum(x, y, x + k - 1, y + k - 1) + calc_sum(x + k, y + k, K, K) + calc_sum(x + k, 1, K, y - 1) + calc_sum(1, y + k, x - 1, K)
				ans = max(ans, val)
			else:
				val = calc_sum(x, 1, x + k - 1, y - k - 1) + calc_sum(1, y - k, x - 1, y - 1) + calc_sum(x + k, y - k, K, y - 1) + calc_sum(x, y, x + k - 1, K)
				ans = max(ans, val)
	print(ans)
main()


def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getArray(intn):
	return [int(input()) for i in range(intn)]

def input():
	return sys.stdin.readline().rstrip()

def rand_N(ran1, ran2):
	return random.randint(ran1, ran2)

def rand_List(ran1, ran2, rantime):
	return [random.randint(ran1, ran2) for i in range(rantime)]

def rand_ints_nodup(ran1, ran2, rantime):
	ns = []
	while len(ns) < rantime:
		n = random.randint(ran1, ran2)
		if not n in ns:
			ns.append(n)
	return sorted(ns)

def rand_query(ran1, ran2, rantime):
	r_query = []
	while len(r_query) < rantime:
		n_q = rand_ints_nodup(ran1, ran2, 2)
		if not n_q in r_query:
			r_query.append(n_q)
	return sorted(r_query)
from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right
import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7
(n, k) = getNM()
imos = [[0 for i in range(k)] for j in range(k)]
white = 0
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'W':
		c = 1
	else:
		c = 0
	x %= 2 * k
	y %= 2 * k
	if x > k - 1:
		x -= k
		c += 1
	if y > k - 1:
		y -= k
		c += 1
	c %= 2
	if c == 1:
		white += 1
		imos[x][y] -= 1
	else:
		imos[x][y] += 1
for i in range(k):
	for j in range(k - 1):
		imos[i][j + 1] += imos[i][j]
for j in range(k):
	for i in range(k - 1):
		imos[i + 1][j] += imos[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		exp = imos[k - 1][k - 1] - imos[k - 1][j] - imos[i][k - 1] + 2 * imos[i][j] + white
		ans = max(ans, max(exp, n - exp))
print(ans)


import numpy as np
import io
(nime, mike) = map(int, input().split())
mod = 2 * mike
array = [[0] * (2 * mike + 1) for _ in range(2 * mike + 1)]

def gen_pattern(x):
	if x < mike - 1:
		return [[0, x + 1], [x + mike + 1, 2 * mike]]
	else:
		return [[x - mike + 1, x + 1]]
for _ in range(nime):
	(x, y, c) = input().split()
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += mike
	x %= mod
	y %= mod
	for tmp in [0, mike]:
		for (l, r) in gen_pattern((x + tmp) % mod):
			for (b, t) in gen_pattern((y + tmp) % mod):
				array[l][b] += 1
				array[l][t] -= 1
				array[r][b] -= 1
				array[r][t] += 1
print(np.max(np.cumsum(np.cumsum(array, axis=0), axis=1)))


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
S = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
dx = [0, 0, K, 2 * K, K, 0, -K, -2 * K, -K, 2 * K, 2 * K, -2 * K, -2 * K]
dy = [0, 2 * K, K, 0, -K, -2 * K, -K, 0, K, 2 * K, -2 * K, 2 * K, -2 * K]

def ch(a):
	ax = a[1]
	ay = a[0]
	if 0 <= ax and ax <= 2 * K - 1:
		if 0 <= ay and ay <= 2 * K - 1:
			return True
	return False

def ch4(a, b, c, d):
	Li = [a, b, c, d]
	for i in range(4):
		if ch(Li[i]):
			return True
	return False

def app(a):
	aa = [0, 0]
	for i in range(2):
		aa[i] = a[i]
		if aa[i] < 0:
			aa[i] = 0
		elif aa[i] > 2 * K - 1:
			aa[i] = 2 * K - 1
	return aa

def paint(a, bw):
	S[a[0]][a[1]] += bw
	return 0
for _ in range(N):
	(sx, sy, sc) = input().split()
	x = int(sx) % (2 * K)
	y = int(sy) % (2 * K)
	if sc == 'W':
		bw = 1
	else:
		bw = -1
	s = (x - (K - 1), y - (K - 1))
	for i in range(len(dx)):
		ld = [s[0] + dx[i], s[1] + dy[i]]
		ru = [ld[0] + (K - 1), ld[1] + (K - 1)]
		lu = [ru[0], ld[1]]
		rd = [ld[0], ru[1]]
		if ch4(ld, ru, lu, rd):
			ld = app(ld)
			ru = app(ru)
			ru[0] += 1
			ru[1] += 1
			lu = [ru[0], ld[1]]
			rd = [ld[0], ru[1]]
			paint(ld, bw)
			paint(ru, bw)
			paint(lu, bw * -1)
			paint(rd, bw * -1)
	if bw == -1:
		S[0][0] += 1
		S[-1][-1] += 1
		S[0][-1] -= 1
		S[-1][0] -= 1
for i in range(2 * K + 1):
	for j in range(2 * K):
		S[i][j + 1] += S[i][j]
for j in range(2 * K + 1):
	for i in range(2 * K):
		S[i + 1][j] += S[i][j]
ans = 0
for i in range(2 * K + 1):
	for j in range(2 * K + 1):
		if S[i][j] > ans:
			ans = S[i][j]
print(ans)


(N, K) = map(int, input().split())
XYC = [[int(x) % (2 * K), int(y) % (2 * K), int(c == 'B')] for (x, y, c) in [input().split() for _ in [0] * N]]
score = [[[0] * (3 * K + 1) for _ in [0] * (3 * K + 1)] for _ in [0] * 2]
for (x, y, c) in XYC:
	x += 1
	y += 1
	score[c][y][x] += 1
	if x < K:
		score[c][y][x + 2 * K] += 1
	if y < K:
		score[c][y + 2 * K][x] += 1
	if x < K and y < K:
		score[c][y + 2 * K][x + 2 * K] += 1
S = score
for c in range(2):
	for j in range(3 * K):
		for i in range(3 * K):
			S[c][i + 1][j + 1] += S[c][i][j + 1]
	for i in range(3 * K):
		for j in range(3 * K):
			S[c][i + 1][j + 1] += S[c][i + 1][j]

def get_sum(c, si, sj, i, j):
	i += 1
	j += 1
	return S[c][i][j] + S[c][si][sj] - S[c][i][sj] - S[c][si][j]
ans = 0
for i in range(K):
	for j in range(2 * K):
		tmp = 0
		tmp += get_sum(0, i, j, i + K - 1, j + K - 1)
		tmp += get_sum(1, i + K, j, i + 2 * K - 1, j + K - 1)
		j = (j + K) % (2 * K)
		tmp += get_sum(1, i, j, i + K - 1, j + K - 1)
		tmp += get_sum(0, i + K, j, i + 2 * K - 1, j + K - 1)
		ans = max(ans, tmp)
print(ans)


(n, k) = map(int, input().split())
a = [(2 * k + 1) * [0] for _ in range(2 * k + 1)]
for _ in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'B':
		y += k
	x %= 2 * k
	y %= 2 * k
	a[x + 1][y + 1] += 1
ans = 0
for i in range(1, 2 * k + 1):
	for j in range(1, 2 * k + 1):
		a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]
for i in range(k, 2 * k + 1):
	for j in range(k, 2 * k + 1):
		t = a[i][j] - a[i - k][j] - a[i][j - k] + a[i - k][j - k]
		t += a[i - k][j - k]
		t += a[2 * k][j - k] - a[i][j - k]
		t += a[i - k][2 * k] - a[i - k][j]
		t += a[2 * k][2 * k] - a[i][2 * k] - a[2 * k][j] + a[i][j]
		ans = max(ans, t, n - t)
print(ans)


import sys
import numpy as np
read = sys.stdin.read
readline = sys.stdin.readline

def main():
	wb = {'W': 1, 'B': 0}
	(n, k) = list(map(int, readline().split()))
	xyc = [l.split() for l in read().splitlines()]
	xy = [[int(lst[0]) % (2 * k), (int(lst[1]) + k * wb[lst[2]]) % (2 * k)] for lst in xyc]
	mat = np.zeros((k + 1, 2 * k + 1), dtype='int64')
	cand = np.zeros((k + 1, k + 1), dtype='int64')
	for e in xy:
		f0 = e[0] >= k
		f1 = e[1] >= k
		mat[e[0] - f0 * k + 1, e[1] + (-1) ** f1 * f0 * k + 1] += 1
	mat = np.cumsum(mat, axis=0)
	mat = np.cumsum(mat, axis=1)
	cand = mat[k, 2 * k] - mat[k, k:2 * k + 1] + mat[k, :k + 1] - np.reshape(mat[:k + 1, 2 * k], (k + 1, 1)) + 2 * mat[:, k:2 * k + 1] - 2 * mat[:, :k + 1]
	print(max(np.max(cand), mat[k, 2 * k] - np.min(cand)))

def __starting_point():
	main()
__starting_point()


(N, K) = (int(i) for i in input().split())
W = list()
for index in range(N):
	(x, y, c) = (s for s in input().split())
	x = int(x)
	y = int(y)
	c = 0 if c == 'W' else 1
	if x % (2 * K) != x % K:
		c = 1 - c
	if y % (2 * K) != y % K:
		c = 1 - c
	W.append((x % K, y % K, c))
w = [[0 for _ in range(K)] for _ in range(K)]
b = [[0 for _ in range(K)] for _ in range(K)]
wx = [0 for _ in range(K)]
wy = [0 for _ in range(K)]
bx = [0 for _ in range(K)]
by = [0 for _ in range(K)]
for (x, y, c) in W:
	if c == 0:
		w[x][y] += 1
		wx[x] += 1
		wy[y] += 1
	else:
		b[x][y] += 1
		bx[x] += 1
		by[y] += 1
M = 0
for row in w:
	M += sum(row)
s = M
for x in range(K):
	tmp = s
	for y in range(K):
		s += by[y]
		s -= wy[y]
		M = max(s, N - s, M)
	for y in range(K):
		by[y] += w[x][y]
		by[y] -= b[x][y]
		wy[y] += b[x][y]
		wy[y] -= w[x][y]
	tmp += bx[x]
	tmp -= wx[x]
	s = tmp
	M = max(s, N - s, M)
print(M)


(n, k) = map(int, input().split())
xyc = [list(input().split()) for i in range(n)]
for i in range(n):
	(x, y, c) = xyc[i]
	x = int(x)
	y = int(y)
	xq = x // k
	yq = y // k
	if (xq + yq) % 2:
		if c == 'B':
			c = 'W'
		else:
			c = 'B'
	x %= k
	y %= k
	xyc[i] = (x, y, c)
gridw = [[0 for i in range(k + 1)] for j in range(k + 1)]
gridb = [[0 for i in range(k + 1)] for j in range(k + 1)]
for (x, y, c) in xyc:
	x += 1
	y += 1
	if c == 'B':
		gridb[x][y] += 1
	else:
		gridw[x][y] += 1
for grid in (gridb, gridw):
	for i in range(1, k + 1):
		for j in range(k + 1):
			grid[i][j] += grid[i - 1][j]
for grid in (gridb, gridw):
	for i in range(k + 1):
		for j in range(1, k + 1):
			grid[i][j] += grid[i][j - 1]
nw = gridw[-1][-1]
nb = gridb[-1][-1]
ans = 0

def sm(grid, i, j):
	return grid[i][j] + grid[-1][-1] - grid[-1][j] - grid[i][-1] + grid[i][j]
for i in range(k + 1):
	for j in range(k + 1):
		x = sm(gridb, i, j) + nw - sm(gridw, i, j)
		ans = max(ans, x, n - x)
print(ans)


(N, K) = map(int, input().split())
G = [[[0, 0] for j in range(2 * K + 1)] for i in range(K + 1)]
W = 0
for i in range(N):
	(x, y, c) = input().split()
	X = int(x) % (2 * K)
	Y = int(y) % (2 * K)
	if X >= K:
		X = (X - K) % K
		Y = (Y - K) % (2 * K)
	G[X + 1][Y + 1][0 if c == 'W' else 1] += 1
	W += 1 if c == 'W' else 0
for i in range(1, K + 1):
	for j in range(1, 2 * K + 1):
		G[i][j][0] += G[i][j - 1][0]
		G[i][j][1] += G[i][j - 1][1]
for i in range(1, K + 1):
	for j in range(1, 2 * K + 1):
		G[i][j][0] += G[i - 1][j][0]
		G[i][j][1] += G[i - 1][j][1]
cnt = 0
for i in range(1, K + 1):
	for j in range(1, K + 1):
		w = 2 * G[i - 1][j - 1][0] + G[K][j + K - 1][0] - 2 * G[i - 1][j + K - 1][0] - G[K][j - 1][0] + G[i - 1][2 * K][0]
		b = 2 * G[i - 1][j - 1][1] + G[K][j + K - 1][1] - 2 * G[i - 1][j + K - 1][1] - G[K][j - 1][1] + G[i - 1][2 * K][1]
		cnt = max([w + (N - W - b), W - w + b, cnt])
print(cnt)


import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint

def ceil(a, b):
	return (a + b - 1) // b
inf = float('inf')
mod = 10 ** 9 + 7

def pprint(*A):
	for a in A:
		print(*a, sep='\n')

def INT_(n):
	return int(n) - 1

def MI():
	return map(int, input().split())

def MF():
	return map(float, input().split())

def MI_():
	return map(INT_, input().split())

def LI():
	return list(MI())

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return list(MF())

def LIN(n: int):
	return [I() for _ in range(n)]

def LLIN(n: int):
	return [LI() for _ in range(n)]

def LLIN_(n: int):
	return [LI_() for _ in range(n)]

def LLI():
	return [list(map(int, l.split())) for l in input()]

def I():
	return int(input())

def F():
	return float(input())

def ST():
	return input().replace('\n', '')

def main():
	(N, K) = MI()
	imos2d = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]

	def get_cord(x):
		if x < K - 1:
			return [(0, x + 1), (x + K + 1, 2 * K)]
		else:
			return [(x - K + 1, x + 1)]
	for _ in range(N):
		(x, y, c) = ST().split()
		(x, y) = (int(x), int(y))
		if c == 'W':
			x += K
		(x, y) = (x % (2 * K), y % (2 * K))
		for base in [0, K]:
			for (l, r) in get_cord((x + base) % (2 * K)):
				for (u, d) in get_cord((y + base) % (2 * K)):
					imos2d[l][u] += 1
					imos2d[l][d] -= 1
					imos2d[r][u] -= 1
					imos2d[r][d] += 1
	for i in range(2 * K):
		for j in range(2 * K + 1):
			imos2d[i + 1][j] += imos2d[i][j]
	for i in range(2 * K + 1):
		for j in range(2 * K):
			imos2d[i][j + 1] += imos2d[i][j]
	ans = 0
	for a in imos2d:
		ans = max(ans, max(a))
	print(ans)
main()


import numpy as np
(n, k) = list(map(int, input().split()))
xyc = [list(input().split()) for _ in range(n)]
b = np.zeros((3 * k - 1, 2 * k - 1), np.int64)
for (x, y, c) in xyc:
	x = int(x)
	y = int(y)
	(q, r) = divmod(x, k)
	if q % 2:
		y += k
	x = r
	if c == 'W':
		y += k
	y %= 2 * k
	b[y][x] += 1
b[:k, k:] = b[k:2 * k, :k - 1]
b[k:2 * k, k:] = b[:k, :k - 1]
b[2 * k:, :] = b[:k - 1, :]
b = np.pad(b, (1, 0), 'constant')
b = np.cumsum(b, axis=0)
b = np.cumsum(b, axis=1)
cnt = b[k:, k:] - b[k:, :-k] - b[:-k, k:] + b[:-k, :-k]
ans = cnt.max()
print(ans)


(N, K) = [int(it) for it in input().split()]
K2 = 2 * K
li = [(lambda x, y, z: [int(x) % K2, int(y) % K2, 0 if z == 'W' else 1])(*input().split()) for i in range(N)]
bm = [[0] * (K2 + 1) for i in range(K2 + 1)]
wm = [[0] * (K2 + 1) for i in range(K2 + 1)]
for (x, y, z) in li:
	if z == 0:
		wm[x + 1][y + 1] += 1
	else:
		bm[x + 1][y + 1] += 1
for i in range(1, 2 * K + 1):
	for j in range(1, 2 * K + 1):
		bm[i][j] = bm[i - 1][j] + bm[i][j - 1] - bm[i - 1][j - 1] + bm[i][j]
		wm[i][j] = wm[i - 1][j] + wm[i][j - 1] - wm[i - 1][j - 1] + wm[i][j]
sumb = bm
sumw = wm
ans = 0
for i in range(K):
	for j in range(K):
		for _ in range(2):
			s = 0
			s += sumb[i][j] - sumb[i][0] - sumb[0][j] + sumb[0][0]
			s += sumb[i + K][j + K] - sumb[i][j + K] - sumb[i + K][j] + sumb[i][j]
			s += sumb[i][2 * K] - sumb[0][2 * K] - sumb[i][j + K] + sumb[0][j + K]
			s += sumb[2 * K][j] - sumb[i + K][j] - sumb[2 * K][0] + sumb[i + K][0]
			s += sumb[2 * K][2 * K] - sumb[i + K][2 * K] - sumb[2 * K][j + K] + sumb[i + K][j + K]
			s += sumw[i][j + K] - sumw[0][j + K] - sumw[i][j] + sumw[0][j]
			s += sumw[i + K][j] - sumw[i][j] - sumw[i + K][0] + sumw[i][0]
			s += sumw[2 * K][j + K] - sumw[i + K][j + K] - sumw[2 * K][j] + sumw[i + K][j]
			s += sumw[i + K][2 * K] - sumw[i + K][j + K] - sumw[i][2 * K] + sumw[i][j + K]
			ans = max(ans, s)
			(sumb, sumw) = (sumw, sumb)
print(ans)


(N, K) = list(map(int, input().split()))
cum = [[0] * (2 * K + 1) for i in range(2 * K + 1)]

def vertex(z):
	if z < K:
		return [(0, z + 1), (z + K + 1, 2 * K)]
	else:
		return [(z - K + 1, z + 1)]
for i in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	x %= 2 * K
	if c == 'B':
		y %= 2 * K
	else:
		y += K
		y %= 2 * K
	for d in [0, K]:
		for (b, t) in vertex((y + d) % (2 * K)):
			for (l, r) in vertex((x + d) % (2 * K)):
				cum[b][l] += 1
				cum[t][r] += 1
				cum[b][r] -= 1
				cum[t][l] -= 1
import numpy as np
cum = np.array(cum)
print(np.cumsum(np.cumsum(cum, axis=0), axis=1).max())


from collections import defaultdict, deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	return list(sys.stdin.readline())[:-1]

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def check(y, x, k):
	l = max(0, min(m, x))
	r = max(0, min(m, x + k))
	d = max(0, min(m, y))
	u = max(0, min(m, y + k))
	f[d][l] += 1
	f[d][r] -= 1
	f[u][l] -= 1
	f[u][r] += 1
(n, k) = LI()
m = k * 2
f = [[0] * (m + 1) for i in range(m + 1)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x) % m
	y = int(y) % m
	if c == 'W':
		y += k
		y %= m
	check(y - m + 1, x - m + 1, k)
	check(y + 1, x - m + 1, k)
	check(y - k + 1, x - k + 1, k)
	check(y + k + 1, x - k + 1, k)
	check(y - m + 1, x + 1, k)
	check(y + 1, x + 1, k)
	check(y - k + 1, x + k + 1, k)
	check(y + k + 1, x + k + 1, k)
for y in range(m + 1):
	for x in range(m):
		f[y][x + 1] += f[y][x]
for y in range(m):
	for x in range(m + 1):
		f[y + 1][x] += f[y][x]
ans = 0
for i in f:
	ans = max(ans, max(i))
print(ans)


import numpy as np
(n, k) = map(int, input().split())
m = np.zeros((3 * k, 3 * k), dtype=int)
l = 2 * k
o = 0
for _ in range(n):
	(x, y, c) = input().split()
	(x, y) = (int(x) % l, int(y) % l)
	t = c == 'W'
	m[x, y] += 1 - 2 * t
	o += t
for _ in range(2):
	m[l:] = m[:k]
	m = np.cumsum(m, axis=0)
	m[:l] = m[k:] - m[:l]
	m = m.T
m = m[:l, :l]
print(o + (m + np.roll(np.roll(m, k, axis=0), k, axis=1)).max())


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10 ** 9))
write = lambda x: sys.stdout.write(x + '\n')
(n, k) = list(map(int, input().split()))
v = [[0] * (2 * k) for _ in range(2 * k)]
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'B':
		x += k
	x %= 2 * k
	y %= 2 * k
	v[x][y] += 1

def cumsum2(v):
	(h, w) = (len(v), len(v[0]))
	v.insert(0, [0] * w)
	for l in v:
		l.insert(0, 0)
	for i in range(1, h + 1):
		for j in range(1, w + 1):
			v[i][j] = v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1] + v[i][j]
	return v
cumsum2(v)

def sub(c, i, j):
	i %= 2 * k
	j %= 2 * k
	l = []
	if i <= k and j <= k:
		l = [[i, i + k, j, j + k]]
	elif i > k and j <= k:
		l = [[i, 2 * k, j, j + k], [0, i - k, j, j + k]]
	elif i <= k and j > k:
		l = [[i, i + k, j, 2 * k], [i, i + k, 0, j - k]]
	else:
		l = [[i, 2 * k, j, 2 * k], [0, i - k, j, 2 * k], [i, 2 * k, 0, j - k], [0, i - k, 0, j - k]]
	return sum((c[z][w] - c[z][y] - c[x][w] + c[x][y] for (x, z, y, w) in l))
ans = -1
for i in range(2 * k):
	for j in range(2 * k - i):
		val0 = sub(v, i, j) + sub(v, i + k, j + k)
		ans = max(ans, val0)
print(ans)


from itertools import chain
import sys
try:
	from typing import List
except ImportError:
	pass

def solve(N: int, K: int, x: 'List[int]', y: 'List[int]', c: 'List[str]'):
	imos = [[0] * (2 * K) for _ in range(2 * K)]

	def sq(x1: int, y1: int, x2: int, y2: int):
		x1 %= 2 * K
		x2 %= 2 * K
		y1 %= 2 * K
		y2 %= 2 * K
		if x1 > x2:
			imos[y1][0] += 1
			imos[y2][0] -= 1
			if y1 > y2:
				imos[0][0] += 1
		if y1 > y2:
			imos[0][x1] += 1
			imos[0][x2] -= 1
		imos[y1][x1] += 1
		imos[y1][x2] -= 1
		imos[y2][x1] -= 1
		imos[y2][x2] += 1
	for (xi, yi, ci) in zip(x, y, c):
		if ci == 'B':
			bx = xi + 1
			by = yi + 1
		else:
			bx = xi - K + 1
			by = yi + 1
		sq(bx, by, bx + K, by + K)
		sq(bx + K, by + K, bx, by)
	for i in range(2 * K):
		for j in range(1, 2 * K):
			imos[i][j] += imos[i][j - 1]
	for j in range(2 * K):
		for i in range(1, 2 * K):
			imos[i][j] += imos[i - 1][j]
	print(max(chain(*imos)))

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	N = int(next(tokens))
	K = int(next(tokens))
	x = [int()] * N
	y = [int()] * N
	c = [str()] * N
	for i in range(N):
		x[i] = int(next(tokens))
		y[i] = int(next(tokens))
		c[i] = next(tokens)
	solve(N, K, x, y, c)
main()


import sys
import array
input = sys.stdin.readline
(N, K) = map(int, input().split())
xyc = []
for i in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	xyc.append([x % (2 * K), y % (2 * K), c])
wmap = [array.array('I', [0] * 3 * K) for i in [0] * 3 * K]
hmap = [array.array('I', [0] * 3 * K) for i in [0] * 3 * K]
for (x, y, c) in xyc:
	if c == 'W':
		wmap[y][x] += 1
		if y < K:
			wmap[y + 2 * K][x] += 1
		if x < K:
			wmap[y][x + 2 * K] += 1
		if x < K and y < K:
			wmap[y + 2 * K][x + 2 * K] += 1
	else:
		hmap[y][x] += 1
		if x < K:
			hmap[y][x + 2 * K] += 1
		if y < K:
			hmap[y + 2 * K][x] += 1
		if x < K and y < K:
			hmap[y + 2 * K][x + 2 * K] += 1

def accum(L):
	res = [0] * (len(L) + 1)
	res[1] = L[0]
	for i in range(2, len(L) + 1):
		res[i] = res[i - 1] + L[i - 1]
	return res
wcumsum = [array.array('I', [0] * (3 * K + 1))] + [array.array('I', accum(row)) for row in wmap]
hcumsum = [array.array('I', [0] * (3 * K + 1))] + [array.array('I', accum(row)) for row in hmap]
for y in range(1, 3 * K + 1):
	for x in range(3 * K + 1):
		wcumsum[y][x] += wcumsum[y - 1][x]
		hcumsum[y][x] += hcumsum[y - 1][x]

def cs2(m, ltx, lty, rbx, rby):
	S = m[rby - 1][rbx - 1]
	S -= m[rby - 1][ltx - 1]
	S -= m[lty - 1][rbx - 1]
	S += m[lty - 1][ltx - 1]
	return S
ans = 0
for y in range(1, K + 1):
	for x in range(1, K + 1):
		ltw = cs2(wcumsum, x, y, x + K, y + K)
		rbw = cs2(wcumsum, x + K, y + K, x + 2 * K, y + 2 * K)
		lbh = cs2(hcumsum, x, y + K, x + K, y + 2 * K)
		rth = cs2(hcumsum, x + K, y, x + 2 * K, y + K)
		temp = ltw + rbw + lbh + rth
		ans = max(ans, temp)
		ltw = cs2(hcumsum, x, y, x + K, y + K)
		rbw = cs2(hcumsum, x + K, y + K, x + 2 * K, y + 2 * K)
		lbh = cs2(wcumsum, x, y + K, x + K, y + 2 * K)
		rth = cs2(wcumsum, x + K, y, x + 2 * K, y + K)
		temp = ltw + rbw + lbh + rth
		ans = max(ans, temp)
print(ans)


import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
	(n, k) = map(int, readline().split())
	l = 2 * k
	grid = [[0] * (2 * l) for _ in range(2 * l)]
	for i in range(n):
		(x, y, c) = input().split()
		(x, y) = (int(x), int(y))
		if c == 'W':
			x += k
		(mx, my) = (x % l, y % l)
		grid[mx][my] += 1
		grid[mx + k][my] -= 1
		grid[mx][my + k] -= 1
		grid[mx + k][my + k] += 1
		grid[mx + k][my + k] += 1
		grid[mx + 2 * k][my + k] -= 1
		grid[mx + k][my + 2 * k] -= 1
		grid[mx + 2 * k][my + 2 * k] += 1
	for x in range(1, 2 * l):
		for y in range(2 * l):
			grid[x][y] += grid[x - 1][y]
	for x in range(2 * l):
		for y in range(1, 2 * l):
			grid[x][y] += grid[x][y - 1]
	count = [[0] * l for _ in range(l)]
	for x in range(2 * l):
		for y in range(2 * l):
			count[x % l][y % l] += grid[x][y]
	ans = 0
	for x in range(l):
		for y in range(l):
			ans = max(ans, count[x][y])
	print(ans)
main()


import numpy as np

def getBoundary(p, k, k2):
	if p - k + 1 >= 0:
		return [(p - k + 1, p + 1)]
	else:
		return [(0, p + 1), ((p - k + 1) % k2, k2)]
(n, k) = list(map(int, input().strip().split()))
k2 = k * 2
b = [[0 for i in range(k2 + 1)] for j in range(k2 + 1)]
for i in range(n):
	(x_str, y_str, c_str) = input().strip().split()
	x = int(x_str) % k2
	y = (int(y_str) + (c_str == 'B') * k) % k2
	for (xb, xe) in getBoundary(x % k2, k, k2):
		for (yb, ye) in getBoundary(y % k2, k, k2):
			b[xb][yb] += 1
			b[xb][ye] -= 1
			b[xe][yb] -= 1
			b[xe][ye] += 1
	for (xb, xe) in getBoundary((x + k) % k2, k, k2):
		for (yb, ye) in getBoundary((y + k) % k2, k, k2):
			b[xb][yb] += 1
			b[xb][ye] -= 1
			b[xe][yb] -= 1
			b[xe][ye] += 1
arr_b = np.array(b)
for i in range(k2):
	arr_b[i + 1] += arr_b[i]
for j in range(k2):
	arr_b[:, j + 1] += arr_b[:, j]
print(arr_b.max())


(N, K) = map(int, input().split())
XYC = [list(input().split()) for i in range(N)]
MAP = [[0 for i in range(K * 2 + 1)] for j in range(K * 2 + 1)]
mod = K * 2
for (x, y, c) in XYC:
	x = int(x)
	y = int(y)
	if c == 'W':
		x += K
	MAP[x % mod + 1][y % mod + 1] += 1
	MAP[(x + K) % mod + 1][(y + K) % mod + 1] += 1
for i in range(K * 2 + 1):
	for j in range(1, K * 2 + 1):
		MAP[i][j] = MAP[i][j - 1] + MAP[i][j]
for i in range(K * 2 + 1):
	for j in range(1, K * 2 + 1):
		MAP[j][i] = MAP[j - 1][i] + MAP[j][i]
ANS = 0
for i in range(K):
	for j in range(K):
		if ANS < MAP[i + K][j + K] - MAP[i][j + K] - MAP[i + K][j] + MAP[i][j]:
			ANS = MAP[i + K][j + K] - MAP[i][j + K] - MAP[i + K][j] + MAP[i][j]
		if ANS < N - (MAP[i + K][j + K] - MAP[i][j + K] - MAP[i + K][j] + MAP[i][j]):
			ANS = N - (MAP[i + K][j + K] - MAP[i][j + K] - MAP[i + K][j] + MAP[i][j])
print(ANS)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 15
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(n, k) = LI()
	aa = [LS() for _ in range(n)]
	a = [[int(_[0]) + (k if _[2] == 'W' else 0), int(_[1])] for _ in aa]
	k2 = k * 2
	aa = [[0] * k2 for _ in range(k2)]
	dr = [0, -2, -1, 0, 1, 2, 1, 0, -1, 2, 2, -2, -2]
	dc = [0, 0, 1, 2, 1, 0, -1, -2, -1, 2, -2, 2, -2]
	for (x, y) in a:
		x %= k2
		y %= k2
		for i in range(13):
			cx = x + dr[i] * k
			cy = y + dc[i] * k
			if cx >= k2 or cy >= k2 or cx <= -k or (cy <= -k):
				continue
			mx = max(cx, 0)
			my = max(cy, 0)
			aa[mx][my] += 1
			if cx < k:
				aa[cx + k][my] -= 1
				if cy < k:
					aa[cx + k][cy + k] += 1
			if cy < k:
				aa[mx][cy + k] -= 1
	for i in range(k2):
		for j in range(1, k2):
			aa[i][j] += aa[i][j - 1]
	for i in range(k2):
		for j in range(1, k2):
			aa[j][i] += aa[j - 1][i]
	return max((max(c) for c in aa))
print(main())


import numpy as np
(n, k) = list(map(int, input().split()))

def get_bound(x):
	if x < k - 1:
		return [(0, x + 1), (x + k + 1, 2 * k)]
	else:
		return [(x - k + 1, x + 1)]
blacks = [[0] * (2 * k + 1) for _ in range(2 * k + 1)]
for _ in range(n):
	(x, y, c) = input().split()
	x = int(x) % (2 * k)
	y = int(y) % (2 * k) if c == 'B' else (int(y) + k) % (2 * k)
	for shift in [0, k]:
		for (b, t) in get_bound((y + shift) % (2 * k)):
			for (l, r) in get_bound((x + shift) % (2 * k)):
				blacks[b][l] += 1
				blacks[t][l] -= 1
				blacks[b][r] -= 1
				blacks[t][r] += 1
ans = np.max(np.cumsum(np.cumsum(blacks, axis=1), axis=0))
print(int(ans))


import sys
input = sys.stdin.readline

def accumrate(h, w, acc, A):
	for i in range(1, h + 1):
		for j in range(1, w + 1):
			acc[i][j] = acc[i][j - 1] + A[i - 1][j - 1]
	for i in range(1, h + 1):
		for j in range(1, w + 1):
			acc[i][j] += acc[i - 1][j]

def cal(i, j, g, r, acc):
	return acc[g][r] + acc[i][j] - acc[g][j] - acc[i][r]
(n, k) = map(int, input().split())
K = 2 * k
cw = [[0] * K for i in range(K)]
cb = [[0] * K for i in range(K)]
sw = 0
sb = 0
for i in range(n):
	(x, y, c) = input().split()
	if c == 'B':
		cb[int(x) % K][int(y) % K] += 1
		sb += 1
	else:
		cw[int(x) % K][int(y) % K] += 1
		sw += 1
acw = [[0] * (K + 1) for i in range(K + 1)]
acb = [[0] * (K + 1) for i in range(K + 1)]
accumrate(K, K, acb, cb)
accumrate(K, K, acw, cw)
ans = 0
for i in range(k):
	for j in range(k):
		chk_b = cal(0, 0, i, j, acb) + cal(i, j, i + k, j + k, acb) + cal(i + k, j + k, K, K, acb) + cal(0, j + k, i, K, acb) + cal(i + k, 0, K, j, acb)
		chk_w = cal(0, 0, i, j, acw) + cal(i, j, i + k, j + k, acw) + cal(i + k, j + k, K, K, acw) + cal(0, j + k, i, K, acw) + cal(i + k, 0, K, j, acw)
		ans = max(ans, max(chk_b + sw - chk_w, chk_w + sb - chk_b))
print(ans)


(n, k) = map(int, input().split())
C = [[0] * (2 * k) for _ in range(2 * k)]
for i in range(n):
	(x, y, c) = map(str, input().split())
	(x, y) = (int(x), int(y))
	if c == 'W':
		y += k
	C[y % (2 * k)][x % (2 * k)] += 1
s = [[0] * (2 * k + 1) for _ in range(2 * k + 1)]
for i in range(2 * k):
	for j in range(2 * k):
		s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + C[i][j]

def count(x1, x2, y1, y2):
	return s[y2][x2] - s[y1][x2] - s[y2][x1] + s[y1][x1]
ans = 0
for i in range(k):
	for j in range(k):
		temp = 0
		temp += count(0, j, 0, i)
		temp += count(j, j + k, i, i + k)
		temp += count(0, j, i + k, 2 * k)
		temp += count(j + k, 2 * k, 0, i)
		temp += count(j + k, 2 * k, i + k, 2 * k)
		ans = max(ans, max(temp, n - temp))
print(ans)


sc = iter(open(0).read().split())
ni = lambda : int(next(sc))
ns = lambda : next(sc)
(N, K) = (ni(), ni())
K2 = K * 2
A = [[0] * K2 for _ in range(K2)]
for _ in range(N):
	(x, y, c) = (ni(), ni(), ns())
	if c == 'W':
		y += K
	A[y % K2][x % K2] += 1
S = [[0] * (K2 + 1) for _ in range(K2 + 1)]
for i in range(K2):
	for j in range(K2):
		S[i + 1][j + 1] = A[i][j] + S[i + 1][j] + S[i][j + 1] - S[i][j]
ans = 0
for i in range(K, K2 + 1):
	for j in range(K, K2 + 1):
		res = S[i][j] + S[i - K][j - K] - S[i - K][j] - S[i][j - K] + S[i - K][j - K] + S[K2][j - K] - S[i][j - K] + S[i - K][K2] - S[i - K][j] + S[K2][K2] + S[i][j] - S[i][K2] - S[K2][j]
		ans = max(ans, res, N - res)
print(ans)


(n, k) = map(int, input().split())
imos = [[0 for i in range(k)] for j in range(k)]
white = 0
for i in range(n):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'W':
		c = 1
	else:
		c = 0
	x %= 2 * k
	y %= 2 * k
	if x > k - 1:
		x -= k
		c += 1
	if y > k - 1:
		y -= k
		c += 1
	c %= 2
	if c == 1:
		white += 1
		imos[x][y] -= 1
	else:
		imos[x][y] += 1
for i in range(k):
	for j in range(k - 1):
		imos[i][j + 1] += imos[i][j]
for j in range(k):
	for i in range(k - 1):
		imos[i + 1][j] += imos[i][j]
ans = 0
for i in range(k):
	for j in range(k):
		exp = imos[k - 1][k - 1] - imos[k - 1][j] - imos[i][k - 1] + 2 * imos[i][j] + white
		ans = max(ans, max(exp, n - exp))
print(ans)


import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy
(N, K) = map(int, input().split())
(X, Y, C) = [[0] * N for _ in range(3)]
mem = [[0] * (4 * K + 1) for _ in range(4 * K + 1)]
for i in range(N):
	S = input().split()
	(X[i], Y[i], C[i]) = (int(S[0]), int(S[1]), S[2])
	(x, y) = (X[i] % (2 * K) + 1, (Y[i] + (K if C[i] == 'W' else 0)) % (2 * K) + 1)
	mem[x][y] += 1
	mem[x + K][y] -= 1
	mem[x][y + K] -= 1
	mem[x + K][y + K] += 1
for h in range(1, 4 * K + 1):
	for w in range(1, 4 * K + 1):
		mem[h][w] += mem[h - 1][w] + mem[h][w - 1] - mem[h - 1][w - 1]
ans = 0
for h in range(K + 1):
	for w in range(K + 1):
		s = mem[h][w] + mem[h + 2 * K][w] + mem[h][w + 2 * K] + mem[h + K][w + K] + mem[h + 2 * K][w + 2 * K]
		ans = max(ans, s)
for h in range(K + 1):
	for w in range(K, 2 * K + 1):
		s = mem[h][w] + mem[h + 2 * K][w] + mem[h + K][w - K] + mem[h + K][w + K]
		ans = max(ans, s)
print(ans)


(N, K) = map(int, input().split())
K2 = 2 * K
A = [[0 for _ in range(K2)] for _ in range(K2)]
for _ in range(N):
	(x, y, c) = input().split()
	(x, y) = (int(x) % K2, int(y) % K2)
	if c == 'W':
		y = (y + K) % K2
	A[x][y] += 1
for i in range(K2):
	A[i] = A[i] + A[i][:K - 1]
for i in range(K - 1):
	A.append(A[i])
B = [[0 for _ in range(K2 + K)] for _ in range(K2 + K)]
for i in range(K2 + K - 1):
	for j in range(K2 + K - 1):
		B[i + 1][j + 1] = B[i + 1][j] + B[i][j + 1] - B[i][j] + A[i][j]
ans = 0
for i in range(K):
	for j in range(K):
		wk = 0
		wk += B[i + K][j + K] - B[i][j + K] - B[i + K][j] + B[i][j]
		wk += B[i + K2][j + K2] - B[i + K][j + K2] - B[i + K2][j + K] + B[i + K][j + K]
		ans = max(wk, N - wk, ans)
print(ans)


def main():
	from itertools import accumulate as ac
	(n, k) = map(int, input().split())
	kk = 2 * k
	xyc = [input().split() for _ in [0] * n]
	b = [[0] * (k + 1) for _ in [0] * (kk + 1)]
	for (x, y, c) in xyc:
		if c == 'B':
			if int(y) % kk < k:
				b[int(x) % kk + 1][int(y) % k + 1] += 1
			else:
				b[(int(x) + k) % kk + 1][int(y) % k + 1] += 1
		elif int(y) % kk < k:
			b[(int(x) + k) % kk + 1][int(y) % k + 1] += 1
		else:
			b[int(x) % kk + 1][int(y) % k + 1] += 1
	b = [list(ac(i)) for i in b]
	for i in range(1, kk + 1):
		for j in range(k + 1):
			b[i][j] += b[i - 1][j]
	ans = 0
	for i in range(kk):
		if i == 0:
			a_t = ((i, i + k, 0), (i + k, kk, 1))
		elif i < k:
			a_t = ((0, i, -1), (i, i + k, 0), (i + k, kk, 1))
		elif i == k:
			a_t = ((0, i, -1), (i, kk, 0))
		else:
			a_t = ((0, i - k, -2), (i - k, i, -1), (i, kk, 0))
		for j in range(k):
			if j == 0:
				b_t = ((0, k, 0),)
			else:
				b_t = ((0, j, -1), (j, k, 0))
			black = []
			for (i, j, l) in a_t:
				for (x, y, z) in b_t:
					if (l + z) % 2 == 0:
						black.append((i, x, j, y))
			ret = 0
			for (tx1, ty1, tx2, ty2) in black:
				ret += b[tx1][ty1] + b[tx2][ty2] - b[tx1][ty2] - b[tx2][ty1]
			ans = max(ans, ret)
	print(ans)
main()


from itertools import accumulate
(N, K) = map(int, input().split())
grid = [[0] * (4 * K + 1) for _ in range(4 * K + 1)]
for _ in range(N):
	(x, y, c) = input().split()
	x = int(x)
	y = int(y)
	if c == 'B':
		x += K
	x %= 2 * K
	y %= 2 * K
	grid[x + 1][y + 1] += 1
	grid[x + 2 * K + 1][y + 1] += 1
	grid[x + 1][y + 2 * K + 1] += 1
	grid[x + 2 * K + 1][y + 2 * K + 1] += 1
for i in range(4 * K):
	grid[i + 1] = list(accumulate(grid[i + 1]))
for i in range(4 * K):
	for j in range(1, 4 * K + 1):
		grid[i + 1][j] += grid[i][j]
ans = 0
for i in range(2 * K):
	for j in range(2 * K):
		x = i
		y = j
		A = grid[x][y] - grid[x + K][y] - grid[x][y + K] + grid[x + K][y + K]
		x += K
		y += K
		B = grid[x][y] - grid[x + K][y] - grid[x][y + K] + grid[x + K][y + K]
		ans = max(ans, A + B)
print(ans)


(n, k) = map(int, input().split())
mp1 = [[0] * k for _ in range(k)]
mp2 = [[0] * k for _ in range(k)]
for _ in range(n):
	(x, y, c) = input().split()
	t = int(x) // k + int(y) // k
	x = int(x) % k
	y = int(y) % k
	if c == 'B':
		t += 1
	if t % 2:
		mp1[y][x] += 1
	else:
		mp2[y][x] += 1
for x in range(1, k):
	mp1[0][x] += mp1[0][x - 1]
	mp2[0][x] += mp2[0][x - 1]
for y in range(1, k):
	acc1 = 0
	acc2 = 0
	for x in range(k):
		acc1 += mp1[y][x]
		mp1[y][x] = acc1 + mp1[y - 1][x]
		acc2 += mp2[y][x]
		mp2[y][x] = acc2 + mp2[y - 1][x]
ans = 0
sum1 = mp1[k - 1][k - 1]
sum2 = mp2[k - 1][k - 1]
mp1k = mp1[k - 1]
mp2k = mp2[k - 1]
for y in range(k):
	mp1y = mp1[y]
	mp1yk = mp1y[k - 1]
	mp2y = mp2[y]
	mp2yk = mp2y[k - 1]
	for x in range(k):
		mp1yx = mp1y[x]
		mp2yx = mp2y[x]
		mp1kx = mp1k[x]
		mp2kx = mp2k[x]
		ans = max(ans, 2 * mp1yx + sum1 - mp1yk - mp1kx + mp2kx + mp2yk - 2 * mp2yx)
		ans = max(ans, 2 * mp2yx + sum2 - mp2yk - mp2kx + mp1kx + mp1yk - 2 * mp1yx)
print(ans)


from itertools import accumulate
import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
black = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
white = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
for _ in range(N):
	(x, y, c) = input().split()
	x = int(x) % (2 * K) + 1
	y = int(y) % (2 * K) + 1
	if c == 'B':
		black[x][y] += 1
	else:
		white[x][y] += 1
for i in range(1, 2 * K + 1):
	black[i] = list(accumulate(black[i]))
	white[i] = list(accumulate(white[i]))
	for j in range(2 * K + 1):
		black[i][j] += black[i - 1][j]
		white[i][j] += white[i - 1][j]
ans = 0
for i in range(K):
	for j in range(K):
		cnt = 0
		for k in [-1, 0, 1]:
			for l in [-1, 0, 1]:
				r = i + K * k
				c = j + K * l
				color = black if (k + l) % 2 == 0 else white
				s1 = color[min(r + K, 2 * K)][min(c + K, 2 * K)]
				s2 = color[min(r + K, 2 * K)][max(c, 0)]
				s3 = color[max(r, 0)][min(c + K, 2 * K)]
				s4 = color[max(r, 0)][max(c, 0)]
				cnt += s1 - s2 - s3 + s4
		ans = max(ans, cnt, N - cnt)
print(ans)


(n, k) = [int(i) for i in input().split(' ')]
dm = [[0 for j in range(0, 2 * k)] for i in range(0, 2 * k)]
s = [[0 for j in range(0, 2 * k + 1)] for i in range(0, 2 * k + 1)]
for i in range(0, n):
	(x, y, c) = input().split(' ')
	x = int(x) % (2 * k)
	if c == 'W':
		y = int(y) % (2 * k)
	else:
		y = (int(y) + k) % (2 * k)
	dm[x][y] += 1
for i in range(1, 2 * k + 1):
	for j in range(1, 2 * k + 1):
		s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + dm[i - 1][j - 1]

def srect(i, j, r, l):
	global s
	return s[r][l] - s[r][j] - s[i][l] + s[i][j]
m = 0
for i in range(0, k):
	for j in range(0, k):
		aw = srect(0, j, i, j + k) + srect(i, 0, i + k, j) + srect(i, j + k, i + k, 2 * k) + srect(i + k, j, 2 * k, j + k)
		raw = n - aw
		if m < max(aw, raw):
			m = max(aw, raw)
print(m)


class Bit:

	def __init__(self, n):
		self.size = n
		self.tree = [0] * (n + 1)

	def sum(self, i):
		s = 0
		while i > 0:
			s += self.tree[i]
			i -= i & -i
		return s

	def add(self, i, x):
		while i <= self.size:
			self.tree[i] += x
			i += i & -i
(N, K) = map(int, input().split())
black = []
white = []
for i in range(N):
	(x, y, c) = map(str, input().split())
	temp = 0
	(dx, x) = divmod(int(x), K)
	temp += dx
	(dy, y) = divmod(int(y), K)
	temp += dy
	color = 0 if c == 'B' else 1
	color = (color + temp) % 2
	if color == 0:
		white.append((x, y))
	else:
		black.append((x, y))
white_bit_list = [0]
black_bit_list = [0]
for _ in range(K):
	white_bit = Bit(K)
	black_bit = Bit(K)
	white_bit_list.append(white_bit)
	black_bit_list.append(black_bit)
white_y = [0] * K
black_y = [0] * K
for xy in white:
	(x, y) = xy
	white_y[y] += 1
	x += 1
	y += 1
	white_bit_list[x].add(y, 1)
for xy in black:
	(x, y) = xy
	black_y[y] += 1
	x += 1
	y += 1
	black_bit_list[x].add(y, 1)
white_y_cum = [0] * (K + 1)
black_y_cum = [0] * (K + 1)
for i in range(K):
	white_y_cum[i + 1] = white_y_cum[i] + white_y[i]
	black_y_cum[i + 1] = black_y_cum[i] + black_y[i]
ans_best = 0
for dy in range(1, K):
	ul_w = 0
	ur_b = black_y_cum[dy]
	ll_b = 0
	lr_w = white_y_cum[-1] - white_y_cum[dy]
	ans = ul_w + ur_b + ll_b + lr_w
	ans_best = max(ans_best, ans, N - ans)
	for dx in range(1, K):
		ul_w += white_bit_list[dx].sum(dy)
		ur_b -= black_bit_list[dx].sum(dy)
		ll_b += black_bit_list[dx].sum(K) - black_bit_list[dx].sum(dy)
		lr_w -= white_bit_list[dx].sum(K) - white_bit_list[dx].sum(dy)
		ans = ul_w + ur_b + ll_b + lr_w
		ans_best = max(ans_best, ans, N - ans)
l_w = 0
r_b = len(black)
ans = l_w + r_b
ans_best = max(ans_best, ans, N - ans)
for dx in range(1, K):
	l_w += white_bit_list[dx].sum(K)
	r_b -= black_bit_list[dx].sum(K)
	ans = l_w + r_b
	ans_best = max(ans_best, ans, N - ans)
print(ans_best)


def main():
	import sys
	input = sys.stdin.readline
	(N, K) = map(int, input().split())
	grid = [[0] * (K * 2) for _ in range(K * 2)]
	for _ in range(N):
		(col, row, color) = input().split()
		col = int(col)
		row = int(row)
		if color == 'W':
			row += K
		row %= K * 2
		col %= K * 2
		grid[row][col] += 1
	tbl = [[0] * (K * 2 + 1) for _ in range(K * 2 + 1)]
	for row in range(K * 2):
		for col in range(K * 2):
			tbl[row + 1][col + 1] = tbl[row + 1][col] + tbl[row][col + 1] - tbl[row][col] + grid[row][col]

	def count(row, col, row2, col2):
		return tbl[row2][col2] - tbl[row][col2] - tbl[row2][col] + tbl[row][col]
	ret = 0
	for row in range(K):
		for col in range(K):
			tmp = count(0, 0, row, col) + count(row, col, row + K, col + K) + count(row + K, col + K, K * 2, K * 2) + count(row + K, 0, K * 2, col) + count(0, col + K, row, K * 2)
			ret = max(ret, tmp, N - tmp)
	print(ret)
main()


def d_checker():
	import numpy as np
	(N, K) = [int(i) for i in input().split()]

	def get_bound(p):
		return [(0, p + 1), (p + K + 1, 2 * K)] if p < K - 1 else [(p - K + 1, p + 1)]
	requests_black = [[0] * (2 * K + 1) for _ in range(2 * K + 1)]
	for _ in range(N):
		(x, y, c) = input().split()
		x = int(x) % (2 * K)
		y = int(y) % (2 * K) if c == 'B' else (int(y) + K) % (2 * K)
		for shift in (0, K):
			for (b, t) in get_bound((y + shift) % (2 * K)):
				for (l, r) in get_bound((x + shift) % (2 * K)):
					requests_black[b][l] += 1
					requests_black[t][l] -= 1
					requests_black[b][r] -= 1
					requests_black[t][r] += 1
	return int(np.max(np.cumsum(np.cumsum(requests_black, axis=1), axis=0)))
print(d_checker())