Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

N, K = map(int, input().split())
K2 = K * 2
Mij = np.zeros((K2 + 1, K2 + 1), dtype=int)

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.cumsum(Mij, axis=0)
Mij = np.cumsum(Mij, axis=1)

print(Mij.max())

Fixed submission:
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())

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

N, K = map(int, input().split())
K2 = K * 2

Mij = np.zeros((K2 + 1, K2 + 1), dtype=int)

diff = [
    [-2, 0],
    [-1, -1],
    [-1, 1],
    [0, 0],
    [1, -1],
    [1, 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

    for y, x in diff:
        di = i + y * K
        dj = j + x * K
        i1 = max(di, 0)
        j1 = max(dj, 0)
        i2 = min(di + K, K2)
        j2 = min(dj + K, K2)
        if i1 >= K2 or j1 >= K2 or i2 <= 0 or j2 <= 0:
            continue
        Mij[i1][j1] += 1
        Mij[i2][j1] -= 1
        Mij[i1][j2] -= 1
        Mij[i2][j2] += 1

# 累積和
Mij = np.cumsum(Mij, axis=0)
Mij = np.cumsum(Mij, axis=1)

print(Mij.max())

Fixed submission:
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())

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

N, K = map(int, input().split())
K2 = K * 2

Mij = np.zeros((K2 + 1, K2 + 1), dtype=int)

diff = [
    [-2, 0],
    [-1, -1],
    [-1, 1],
    [0, 0],
    [1, -1],
    [1, 1],
]

def imos(i, j, i2, j2):
    Mij[i][j] += 1
    Mij[i2][j] -= 1
    Mij[i][j2] -= 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

    for y, x in diff:
        di = y * K
        dj = x * K
        i1 = max(i + di, 0)
        j1 = max(j + dj, 0)
        i2 = min(i + di + K, K2)
        j2 = min(j + dj + K, K2)
        if i1 >= K2 or j1 >= K2 or i2 <= 0 or j2 <= 0:
            continue
        imos(i1, j1, i2, j2)

# 累積和
for i in range(K2):
    Mij[i + 1, :] += Mij[i, :]
for j in range(K2):
    Mij[:, j + 1] += Mij[:, j]

print(Mij.max())

Fixed submission:
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())

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, K = map(int, input().split())
Si = [input().split() for _ in range(N)]

K2 = K * 2

Gij = [None] * N
for idx, s in enumerate(Si):
    i = int(s[0]) % K2
    j = int(s[1]) % K2
    if s[2] == "B":
        i = (i + K) % K2
    Gij[idx] = [i, j]

Mij = np.zeros((K2 + 1, K2 + 1), dtype=int)

def imos(i, j, i2, j2):
    Mij[i][j] += 1
    Mij[i2][j] -= 1
    Mij[i][j2] -= 1
    Mij[i2][j2] += 1

for i, j in Gij:
    #
    for y in range(5):
        for x in range(5):
            if (y + x) % 2 > 0:
                continue
            di = (y - 2) * K
            dj = (x - 2) * K
            i1 = max(i + di, 0)
            j1 = max(j + dj, 0)
            i2 = min(i + di + K, K2)
            j2 = min(j + dj + K, K2)
            if i1 >= K2 or j1 >= K2 or i2 <= 0 or j2 <= 0:
                continue
            imos(i1, j1, i2, j2)

# 累積和
for i in range(K2):
    Mij[i + 1, :] += Mij[i, :]
for j in range(K2):
    Mij[:, j + 1] += Mij[:, j]

print(Mij.max())


Fixed submission:
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())