import Control.Applicative
import Control.Monad
import Data.Function
import qualified Data.Map.Lazy as ML

main :: IO()
main = do
    [n,k] <- map read . words <$> getLine
    hopes <- replicateM n $ myread k . words <$> getLine
    print $ mymax 0 n $ map (mycount k (makeTable hopes)) [[x,y] | x <- [0..k-1], y <- [0..k-1]]
        where
            myread k [x,y,b] = [(read x `mod` (2*k), read y `mod` (2*k)), if b == "B" then (1,0) else (0,1)]
            makeTable = foldr (\[p,c] m -> ML.insertWith pairPlus p c m) ML.empty
                where pairPlus (x,y) (x',y') = (x + x',y + y')
            mycount k tab [x0,y0] = ML.foldrWithKey (\p c s -> if f p then s + fst c else s + snd c) 0 tab
                where f (x,y) = odd $ div (x - x0) k + div (y - y0) k
            mymax m _ [] = m
            mymax m k (n:ns) = if n == k then k else mymax (max m n) k ns
