Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    m = defaultdict(int)
    for x, y, c in xyc:
        y += c * K
        x1, y1 = x % K2, y % K2
        x2, y2 = x1 + K, y1 + K
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x2, y1] -= 1
        m[x1, y2] -= 1
        x1, y1 = (x + K) % K2, (y + K) % K2
        x2, y2 = x1 + K, y1 + K
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x2, y1] -= 1
        m[x1, y2] -= 1

    ms = [[0] * (K2 + 1) for i in range(K2 + 1)]
    for y in range(0, K2):
        ms0 = ms[y]
        ms1 = ms[y - 1]
        for x in range(0, K2):
            ms0[x] = m[x, y] + ms0[x-1] + ms1[x] - ms1[x-1]
    
    rmax = 0
    for y in range(K, K2):
        for x in range(K, K2):
            rcount = ms[y][x]
            rmax = max(rmax, rcount, N - rcount)

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))


Fixed submission:
def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    p = [[0] * K2 for i in range(K2)]
    for x, y, c in xyc:
        y += c * K
        p[x % K2][y % K2] += 1
        p[(x + K) % K2][(y + K) % K2] += 1

    def isum(xs):
        r = 0
        yield r
        for x in xs:
            r += x
            yield r

    psum = [list(isum(row)) for row in p]
    pksum = [[b - a for a, b in zip(row, row[K:])] for row in psum]

    rmax = N / 2
    for y in range(K):
        rcount = sum(row[y] for row in pksum[:K])
        for x in range(K):
            rmax = max(rmax, rcount, N - rcount)
            rcount += -pksum[x][y] + pksum[x + K][y]

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    m = defaultdict(int)
    for x, y, c in xyc:
        y += c * K
        x1, y1 = x % K2, y % K2
        x2, y2 = x1 + K, y1 + K
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x2, y1] -= 1
        m[x1, y2] -= 1
        x1, y1 = (x + K) % K2, (y + K) % K2
        x2, y2 = x1 + K, y1 + K
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x2, y1] -= 1
        m[x1, y2] -= 1

    ms = [[0] * (K2 + 1) for i in range(K2 + 1)]
    for y in range(0, K2):
        ms0 = ms[y]
        ms1 = ms[y - 1]
        for x in range(0, K2):
            ms0[x] = m[x, y] + ms0[x-1] + ms1[x] - ms1[x-1]
    
    rmax = 0
    for y in range(K, K2):
        for x in range(K, K2):
            rcount = ms[y][x]
            rmax = max(rmax, rcount, N - rcount)

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))


Fixed submission:
def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    p = [[0] * K2 for i in range(K2)]
    for x, y, c in xyc:
        y += c * K
        p[x % K2][y % K2] += 1
        p[(x + K) % K2][(y + K) % K2] += 1

    def isum(xs):
        r = 0
        yield r
        for x in xs:
            r += x
            yield r

    psum = [list(isum(row)) for row in p]
    pksum = [[b - a for a, b in zip(row, row[K:])] for row in psum]

    rmax = N / 2
    for y in range(K):
        rcount = sum(row[y] for row in pksum[:K])
        for x in range(K):
            rmax = max(rmax, rcount, N - rcount)
            rcount += -pksum[x][y] + pksum[x + K][y]

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


def calc(K, xyc):
    K2 = K * 2
    K3 = K * 3
    K4 = K * 4
    xys = [(x % K2, (y + c * K) % K2) for x, y, c in xyc]

    m = defaultdict(int)
    for x, y in xys:
        x1, y1 = x, y
        x2, y2 = (x1 + K), (y1 + K)
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x1, y2] -= 1
        m[x2, y1] -= 1

        x1, y1 = x, y + K2
        x2, y2 = (x1 + K), (y1 + K)
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x1, y2] -= 1
        m[x2, y1] -= 1

        x1, y1 = x + K2, y
        x2, y2 = (x1 + K), (y1 + K)
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x1, y2] -= 1
        m[x2, y1] -= 1

        x1, y1 = x + K2, y + K2
        x2, y2 = (x1 + K), (y1 + K)
        m[x1, y1] += 1
        m[x2, y2] += 1
        m[x1, y2] -= 1
        m[x2, y1] -= 1


    mt = defaultdict(int)
    for y in range(K4):
        for x in range(K4):
            mt[x, y] = m[x, y] + mt[(x - 1), y] + mt[x, (y - 1)] - mt[(x - 1), (y - 1)]

    rmax = 0
    for y in range(K, K3):
        for x in range(K, K3):
            rcount = mt[x + K, y + K] + mt[x, y]
            rmax = max(rmax, rcount)

    return rmax

N, K = [int(e) for e in input().split()]
xyc = [input().split() for i in range(N)]
xyc = [(int(x), int(y), c == "W") for x, y, c in xyc]

print(calc(K, xyc))


Fixed submission:
def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    p = [[0] * K2 for i in range(K2)]
    for x, y, c in xyc:
        y += c * K
        p[x % K2][y % K2] += 1
        p[(x + K) % K2][(y + K) % K2] += 1

    def isum(xs):
        r = 0
        yield r
        for x in xs:
            r += x
            yield r

    psum = [list(isum(row)) for row in p]
    pksum = [[b - a for a, b in zip(row, row[K:])] for row in psum]

    rmax = N / 2
    for y in range(K):
        rcount = sum(row[y] for row in pksum[:K])
        for x in range(K):
            rmax = max(rmax, rcount, N - rcount)
            rcount += -pksum[x][y] + pksum[x + K][y]

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))

Buggy python submission (verdict: Time Limit Exceeded):
def calc0(K, xyc):
    rmax = 0
    for i in range(K * 2):
        for j in range(K * 2):
            rcount = 0
            for x, y, c in xyc:
                rcount += ((x + j) // K + (y + i) // K) % 2 == c
            rmax = max(rmax, rcount)
    return rmax

def calc1(K, xyc):
    rmax = 0
    for i in range(K * 2):
        xs = [(x + c * K + (y + i) // K * K) % (2 * K) for x, y, c in xyc]
        for j in range(K * 2):
            rcount = 0
            for x in xs:
                rcount += ((x + j) // K)  % 2 == 1
            rmax = max(rmax, rcount)
    return rmax

def calc(K, xyc):
    rmax = 0
    for i in range(K):
        ts = [0 for x in range(K * 2)]
        for x, y, c in xyc:
            a = (x + c * K + (y + i) // K * K) % (2 * K)
            ts[a] += 1
        rcount = sum(ts[0:K])
        for j in range(K):
            rcount += -ts[j] + ts[j+K]
            rmax = max(rmax, rcount, N - rcount)
    return rmax

#import random
#N = 10000
#K = 20
#xyc = [(random.randint(0, 10000), random.randint(0, 10000), random.randint(0, 10000) % 2) for i in range(N)]

N, K = [int(e) for e in input().split()]
xyc = [input().split() for i in range(N)]
xyc = [(int(x), int(y), c == "W") for x, y, c in xyc]

#print(calc0(K, xyc))
#print(calc1(K, xyc))
print(calc(K, xyc))



Fixed submission:
def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    p = [[0] * K2 for i in range(K2)]
    for x, y, c in xyc:
        y += c * K
        p[x % K2][y % K2] += 1
        p[(x + K) % K2][(y + K) % K2] += 1

    def isum(xs):
        r = 0
        yield r
        for x in xs:
            r += x
            yield r

    psum = [list(isum(row)) for row in p]
    pksum = [[b - a for a, b in zip(row, row[K:])] for row in psum]

    rmax = N / 2
    for y in range(K):
        rcount = sum(row[y] for row in pksum[:K])
        for x in range(K):
            rmax = max(rmax, rcount, N - rcount)
            rcount += -pksum[x][y] + pksum[x + K][y]

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = [int(e) for e in input().split()]
xyc = [input().split() for i in range(N)]
xyc = [(int(x), int(y), c == "W") for x, y, c in xyc]

def calc0(K, xyc):
    rmax = 0
    for i in range(K * 2):
        for j in range(K * 2):
            rcount = 0
            for x, y, c in xyc:
                rcount += ((x + j) // K + (y + i) // K) % 2 == c
            rmax = max(rmax, rcount)
    return rmax

def calc1(K, xyc):
    rmax = 0
    for i in range(K * 2):
        xs = [(x + c * K + (y + i) // K * K) % (2 * K) for x, y, c in xyc]
        for j in range(K * 2):
            rcount = 0
            for x in xs:
                rcount += ((x + j) // K)  % 2 == 1
            rmax = max(rmax, rcount)
    return rmax

def calc(K, xyc):
    rmax = 0
    for i in range(K):
        xs = [(x + c * K + (y + i) // K * K) % (2 * K) for x, y, c in xyc]
        ts = [0 for x in range(K * 4)]
        for x in xs:
            ts[x] += 1
            ts[x + K * 2] += 1
        rcount = sum(ts[0:K])
        for j in range(K):
            rcount += -ts[j] + ts[j+K]
            rmax = max(rmax, rcount, N - rcount)
    return rmax

#import random
#N = 10000
#K = 1000
#xyc = [(random.randint(0, 10000), random.randint(0, 10000), random.randint(0, 10000) % 2) for i in range(N)]

#print(calc0(K, xyc))
#print(calc1(K, xyc))
print(calc(K, xyc))



Fixed submission:
def calc(K, xyc):
    K2 = K * 2
    N = len(xyc)

    p = [[0] * K2 for i in range(K2)]
    for x, y, c in xyc:
        y += c * K
        p[x % K2][y % K2] += 1
        p[(x + K) % K2][(y + K) % K2] += 1

    def isum(xs):
        r = 0
        yield r
        for x in xs:
            r += x
            yield r

    psum = [list(isum(row)) for row in p]
    pksum = [[b - a for a, b in zip(row, row[K:])] for row in psum]

    rmax = N / 2
    for y in range(K):
        rcount = sum(row[y] for row in pksum[:K])
        for x in range(K):
            rmax = max(rmax, rcount, N - rcount)
            rcount += -pksum[x][y] + pksum[x + K][y]

    return rmax

if __name__ == '__main__':
    N, K = [int(e) for e in input().split()]
    xyc = [(int(x), int(y), c == "W") for x, y, c in [input().split() for i in range(N)]]
    print(calc(K, xyc))