import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Data.Ord

main :: IO ()
main = breads >>= main'
    where
        main' [n, k] = replicateM n readf >>= print . solve k
            where
                readf = edit . words <$> getLine
                edit [x, y, c] = (read x, read y, c == "W")

solve :: Int -> [(Int, Int, Bool)] -> Int
solve k ys = maximum [calc x y | x <- [0..(k - 1)], y <- [0..(k - 1)]]
    where
        xs = mf . group . sort $ fmap (\(x, y, m) -> (x `mod` (k * 2), y `mod` (k * 2), m)) ys
            where
                mf ( a@((ax, ay, am) : _) : b@((bx, by, _) : _) : zs)
                    | ax == bx && ay == by = (ax, ay, length b, length a) : mf zs
                    | am = (ax, ay, 0, length a) : mf (b : zs)
                    | otherwise = (ax, ay, length a, 0) : mf (b : zs)
                mf (a@((ax, ay, am) : _) : _)
                    | am = [(ax, ay, 0, length a)]
                    | otherwise = [(ax, ay, length a, 0)]
                mf _ = []

        calc dx dy = max (we + bo) $ wo + be
            where
                (we, wo, be, bo) = foldl' ff (0, 0, 0, 0) xs
                ff (a, b, c, d) (x, y, m, n)
                    | even val = (a + m, b, c + n, d)
                    | otherwise = (a, b + m, c, d + n)
                    where
                        val = (x + dx) `div` k + (y + dy) `div` k

breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)