import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad 
import Control.Monad.ST
import Control.Applicative
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV

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

solve :: Int -> [(Int, Int, Bool)] -> Int 
solve k xs = maximum [calc x y | x <- [0..(k - 1)], y <- [0..(k - 1)]]
    where
        k2 = k * 2 - 1

        calc i j
            | i == j && i == k - 1 = cf1 i j 
            | i == k - 1 = cf2 i j
            | j == k - 1 = cf3 i j
            | otherwise = cf4 i j

        cf1 i j = max (v0 + v3) $ v1 + v2
            where
                v0 = vs V.! i V.! j
                v1 = -v0 + vs V.! k2 V.! j 
                v2 = -v0 + vs V.! i V.! k2 
                v3 = -v0 - v1 - v2 + vs V.! k2 V.! k2
        cf2 i j = max (v0 + v3 + v4) $ v1 + v2 + v5
            where
                v0 = vs V.! i V.! j
                v1 = -v0 + vs V.! k2 V.! j
                v2 = -v0 + vs V.! i V.! (j + k)
                v3 = -v0 - v1 - v2 + vs V.! k2 V.! (j + k) 
                v4 = -v0 - v2 + vs V.! i V.! k2
                v5 = -v0 - v1 - v2 - v3 - v4 + vs V.! k2 V.! k2
        cf3 i j = max (v0 + v2 + v4) $ v1 + v3 + v5
            where
                v0 = vs V.! i V.! j
                v1 = -v0 + vs V.! (i + k) V.! j
                v2 = -v0 - v1 + vs V.! k2 V.! j
                v3 = -v0 + vs V.! i V.! (j + k)
                v4 = -v0 - v1 - v3 + vs V.! (i + k) V.! (j + k)
                v5 = -v0 - v1 - v2 - v3 - v4 + vs V.! k2 V.! k2 
        cf4 i j = max (v0 + v2 + v4 + v6 + v8) $ v1 + v3 + v5 + v7
            where
                v0 = vs V.! i V.! j 
                v1 = -v0 + vs V.! (i + k) V.! j
                v2 = -v0 - v1 + vs V.! k2 V.! j
                v3 = -v0 + vs V.! i V.! (j + k)
                v4 = -v0 - v1 - v3 + vs V.! (i + k) V.! (j + k)
                v5 = -v0 - v1 - v2 - v3 - v4 + vs V.! k2 V.! (j + k)
                v6 = -v0 - v3 + vs V.! i V.! k2
                v7 = -v0 - v1 - v3 - v4 - v6 + vs V.! (i + k) V.! k2
                v8 = -v0 - v1 - v2 - v3 - v4 - v5 - v6 - v7 + vs V.! k2 V.! k2

        vs = runST $ makeVec >>= fill >>= sums >>= freeze
        
        makeVec :: ST s (MV.STVector s (MV.STVector s Int))
        makeVec = MV.replicateM (k * 2) $ MV.replicate (k * 2) 0

        fill v = v <$ mapM_ ff ys
            where
                ff (x, j, m) = MV.read v i >>= (\yv -> MV.modify yv succ j)
                    where
                        i
                            | m = x
                            | otherwise = (x + k) `mod` (2 * k)

                ys = fmap (\(x, y, m) -> (x `mod` (2 * k), y `mod` (2 * k), m)) xs

        sums v = v <$ (mapM_ vinit [1..(k - 1)] >> sequence_ [vfill i j | i <- [1..(k - 1)], j <- [1..(k - 1)]])
            where
                vinit i = do 
                    pyv <- MV.read v $ i - 1
                    pn <- MV.read pyv 0
                    yv <- MV.read v i
                    MV.modify yv (+ pn) 0

                    yv <- MV.read v 0
                    pn <- MV.read yv $ i - 1
                    MV.modify yv (+ pn) i

                vfill i j = do 
                    yv <- MV.read v $ i - 1
                    v0 <- MV.read yv $ j - 1
                    v1 <- MV.read yv j 
                    yv <- MV.read v i
                    v2 <- MV.read yv $ j - 1
                    MV.modify yv (\v -> v + v1 + v2 - v0) j

        freeze v = V.unsafeFreeze v >>= V.mapM V.unsafeFreeze

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)