
n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
b=[[0]*k for _ in range(k)]
w=[[0]*k for _ in range(k)]
anb=[[0]*k for _ in range(k)]
anw=[[0]*k for _ in range(k)]
ans=0
i=0
while i<n:
    m=(((int(l[i][0]))//k)+((int(l[i][1]))//k))%2
    if (m==0 and str(l[i][2])=="W"):
        w[int(l[i][0])%k][int(l[i][1])%k]+=1 
    elif (m==1 and str(l[i][2])=="B"):
        b[int(l[i][0])%k][int(l[i][1])%k]+=1
    elif (m==1 and str(l[i][2])=="W"):
        anw[int(l[i][0])%k][int(l[i][1])%k]+=1
    else:
        anb[int(l[i][0])%k][int(l[i][1])%k] +=1

from numpy import matrix

w=matrix(w)
b=matrix(b)
anw=matrix(anw)
anb=matrix(anb)

for a in range(0,k):
     for b in range(0,k):
         total=w[a:k][b:k].sum()+anw[0:a][0:b].sum()+b[a:k][b:k].sum()+anb[0:a][0:b].sum()
         ans=max(ans,total,n-total)
print(ans)
