import qualified Data.ByteString.Char8 as BC
import Data.Maybe (fromJust)
import Data.Array.IO
import Data.List
import Control.Monad

type Arr = IOUArray (Int, Int) Int
type PointC = (Int, Int, Char)
type Point = (Int, Int)

main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  t <- map (compression k) <$> getInt2DListBC
  arr <- newArray ((0, 0), (4*k-1, 4*k-1)) 0 :: IO Arr
  cum <- newArray ((0, 0), (4*k, 4*k)) 0 :: IO Arr
  let idx = [(i, j) | i <- [1..4*k], j <- [1..4*k]]
      pos = [(i, j) | i <- [1..2*k], j <- [1..2*k]]
  foldM (count k) arr t
  foldM (accum2D arr) cum idx
  ans <- maximum <$> mapM (query k cum) pos
  print ans
  return ()

--[(0,0), (i,j))
query :: Int -> Arr -> Point -> IO Int
query k cum (i, j) = do
  a <- readArray cum (i, j)
  b <- readArray cum (i+k, j)
  c <- readArray cum (i, j+k)
  d <- readArray cum (i+k, j+k)
  e <- readArray cum (i+2*k, j+k)
  f <- readArray cum (i+k, j+2*k)
  g <- readArray cum (i+2*k, j+2*k)
  return $ (a + 2 * d + g) - (b + c + e + f)

accum2D :: Arr -> Arr -> Point -> IO Arr
accum2D arr cum (i, j) = do
  x <- readArray cum (i-1, j)
  y <- readArray cum (i, j-1)
  z <- readArray cum (i-1, j-1)
  w <- readArray arr (i-1, j-1)
  writeArray cum (i, j) $ x + y - z + w
  return cum

compression :: Int -> PointC -> Point
compression k (x, y, 'B') = (mod x $ 2*k, mod y $ 2*k)
compression k (x, y, 'W') = (mod x $ 2*k, mod (y+k) $ 2*k)

count :: Int -> Arr -> Point -> IO Arr
count k arr (x,y) = do
  inc arr (x, y)
  inc arr (x+2*k, y)
  inc arr (x, y+2*k)
  inc arr (x+2*k, y+2*k)
  return arr

inc :: Arr -> (Int, Int) -> IO ()
inc arr p = do
  x <- readArray arr p
  writeArray arr p $ x + 1

bsToInt :: BC.ByteString -> Int
bsToInt = fst . fromJust . BC.readInt

getInt2DListBC :: IO [PointC]
getInt2DListBC = map ((\[x,y,z] -> (bsToInt x, bsToInt y, head $ BC.unpack z)) . BC.words) . BC.lines <$> BC.getContents
