{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Data.Array.IO
import Data.Array
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector.Unboxed.Mutable as V
import Data.Maybe

readIntBS = fst . fromJust . BS.readInt
modifyArray arr i f = readArray arr i >>= \x -> writeArray arr i (f x)

main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]

  field <- V.replicate ((2*k+1)*(2*k+1)) 0 :: IO (V.MVector RealWorld Int)

  let
    readV field (x,y) = V.read field (x+y*(2*k+1))
    writeV field (x,y) a = V.write field (x+y*(2*k+1)) a
    modifyV field (x,y) f = readV field (x,y) >>= \a -> writeV field (x,y) (f a)
    func arr x0 y0 x1 y1 = do
      a <- readV arr (x1,y1)
      b <- readV arr (x1,y0-1)
      c <- readV arr (x0-1,y1)
      d <- readV arr (x0-1,y0-1)
      return $ a-b-c+d

  replicateM_ n $ do
    (x,y,c) <- (\[x,y,c] -> (readIntBS x, readIntBS y, BS.head c)) . BS.words <$> BS.getLine :: IO (Int,Int,Char)
    modifyV field ((if c=='B' then x else x+k)`mod`(2*k)+1, y`mod`(2*k)+1) (+1)


  forM_ [1..2*k] $ \j -> do
    forM_ [2..2*k] $ \i -> do
      readV field (i-1,j) >>= \x -> modifyV field (i,j) (x+)

  forM_ [1..2*k] $ \i -> do
    forM_ [2..2*k] $ \j -> do
      readV field (i,j-1) >>= \x -> modifyV field (i,j) (x+)

  print =<< maximum . map (\x -> max x (n-x)) <$>
    (forM [(x,y) | x<-[1..k], y<-[1..k]] $ \(x,y) -> do
        sum <$> sequence
          [func field 1 1 (x-1) (y-1),
           func field x y (x+k-1) (y+k-1),
           func field (x+k) (y+k) (2*k) (2*k),
           func field 1 (y+k) (x-1) (2*k),
           func field (x+k) 1 (2*k) (y-1)]
    )
