Buggy python submission (verdict: Time Limit Exceeded):
def double_accumulate(a):
    h = len(a)
    w = len(a[0])
    b = [[0]*(w+1) for i in range(h+1)]
    b[1][1] = a[0][0]
    for i in range(h):
        for j in range(w):
            b[i+1][j+1] = b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j]
    return tuple(tuple(x) for x in b)


def double_sum(b,p,q):
    return b[q[0]+1][q[1]+1]-b[q[0]+1][p[1]]-b[p[0]][q[1]+1]+b[p[0]][p[1]]


n,k = map(int, input().split())

kk = k*2

black = [[0]*kk for i in range(kk)]
white = [[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 == 'B':
        black[x][y]+=1
    else:
        white[x][y]+=1

black = tuple(tuple(black[i%kk]*2) for i in range(kk*2))
white = tuple(tuple(white[i%kk]*2) for i in range(kk*2))

B = double_accumulate(black)
W = double_accumulate(white)

ans = 0
for i in range(kk):
    for j in range(kk):
        b = double_sum(B,[i,j],[i+k-1,j+k-1])\
            + double_sum(B,[i+k,j+k],[i+kk-1,j+kk-1])
        w = double_sum(W,[i,j+k],[i+k-1,j+kk-1])\
            + double_sum(W,[i+k,j],[i+kk-1,j+k-1])
        ans = max(ans,b+w)

print(ans)


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

Buggy python submission (verdict: Time Limit Exceeded):
def double_accumulate(a):
    h = len(a)
    w = len(a[0])
    b = [[0]*(w+1) for i in range(h+1)]
    b[1][1] = a[0][0]
    for i in range(h):
        for j in range(w):
            b[i+1][j+1] = b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j]
    return tuple(tuple(x) for x in b)

def double_sum(b,p,q):
    y1,x1 = p
    y2,x2 = q
    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

black = [[0]*kk for i in range(kk)]
white = [[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 == 'B':
        black[x][y]+=1
    else:
        white[x][y]+=1

black = tuple(tuple(black[i%kk]*2) for i in range(kk*2))
white = tuple(tuple(white[i%kk]*2) for i in range(kk*2))

B = double_accumulate(black)
W = double_accumulate(white)

ans = 0
for i in range(kk):
    for j in range(kk):
        b = double_sum(B,[i,j],[i+k-1,j+k-1])\
            + double_sum(B,[i+k,j+k],[i+kk-1,j+kk-1])
        w = double_sum(W,[i,j+k],[i+k-1,j+kk-1])\
            + double_sum(W,[i+k,j],[i+kk-1,j+k-1])
        ans = max(ans,b+w)

print(ans)


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