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()))

#!/usr/bin/env python3

# coding=utf-8



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 = np.zeros((2 * k, 2 * k), dtype=np.int32)

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()))

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()))

#!/usr/bin/env python3
# coding=utf-8
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 = np.zeros((2 * k, 2 * k), dtype=np.int32)
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()))

+#!/usr/bin/env python3 +# coding=utf-8 +import sys -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)] +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 = np.zeros((2 * k, 2 * k), dtype=np.int32) +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 -        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())) +        _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()))