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

type Arr = IOUArray (Int, Int) Int
type UArr = UArray (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
  --cum <- newArray ((0, 0), (2*k, 2*k)) 0 :: IO Arr
  --foldM (count k) cum t
  --accum2D k cum (1, 1)
  cum <- subMain k
  let cum' = listArray ((0, 0), (2*k, 2*k)) cum :: UArr
      pos = [(i, j) | i <- [1..k], j <- [1..k]] ++ [(i, j) | i <- [k+1..2*k], j <- [1..k]]
  --foldM (solve k cum') ans pos
  --x <- maximum <$> getElems ans
  let ans = maximum $ map (solve k cum') pos
  print ans

subMain :: Int -> IO [Int]
subMain k = do
  t <- map (compression k) <$> getInt2DListBC
  cum <- newArray ((0, 0), (2*k, 2*k)) 0 :: IO Arr
  foldM (count k) cum t
  accum2D k cum (1, 1)
  getElems cum

solve :: Int -> UArr -> Point -> Int
solve k cum (i, j)
  | i+k <= 2*k && j+k <= 2*k =
    let a = query cum (i, j, i+k, j+k)
        b = query cum (0, j+k, i, 2*k)
        c = query cum (i+k, 0, 2*k, j)
        d = query cum (i+k, j+k, 2*k, 2*k)
        e = query cum (0, 0, i, j)
    in a + b + c + d + e
  | otherwise =
    let a = query cum (i, j, 2*k, j+k)
        b = query cum (i-k, j+k, i, 2*k)
        c = query cum (0, j, i-k, j+k)
        d = query cum (i-k, 0, i, j)
    in a + b + c + d

query :: UArr -> (Int, Int, Int, Int) -> Int
query cum (i, j, k, l) =
  let a = cum ! (i, j)
      b = cum ! (i, l)
      c = cum ! (k, j)
      d = cum ! (k, l)
  in a + d - b - c

next :: Int -> Point -> Point
next k (x, y)
  | y == 2*k = (x+1, 1)
  | otherwise = (x, y+1)

accum2D :: Int -> Arr -> Point -> IO ()
accum2D k cum (i, j)
  | i == 2*k+1 = return ()
  | otherwise = do
    x <- readArray cum (i-1, j)
    y <- readArray cum (i, j-1)
    z <- readArray cum (i-1, j-1)
    w <- readArray cum (i, j)
    writeArray cum (i, j) $ x + y - z + w
    accum2D k cum $ next k (i, j)

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+1, y+1)
  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
