n,k=map(int,input().split())
f=[]
for a in range(0,k):
     for b in range(0,k):
         i=0
         total=0
         while i<n:
           x1,y1,z=map(str,input().split())
           x,y=map(int,[x1,y1])
           l=(abs(x+y-a-b)//k)%2
           if (l==0 and z=="W") or (l==1 and z=="B"):
               total=total+1
               i=i+1
           else:
               i=i+1

         else:
              f.append(max([total,n-total]))
print(max(f))
