;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  ;; (return-from read-fixnum 0)
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(declaim (inline get-2dcumul))
(defun get-2dcumul (table y0 x0 y1 x1)
  (+ (- (aref table y1 x1)
        (aref table y0 x1)
        (aref table y1 x0))
     (aref table y0 x0)))

(declaim (inline fast-read-char))
(defun fast-read-char (&optional (in *standard-input*))
  (declare (inline read-byte))
  #-swank (code-char (read-byte in))
  #+swank (read-char in))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (btable (make-array (list (+ 1 (* 3 k)) (+ 1 (* 3 k)))
                             :element-type 'uint32 :initial-element 0))
         (wtable (make-array (list (+ 1 (* 3 k)) (+ 1 (* 3 k)))
                             :element-type 'uint32 :initial-element 0)))
    (declare (uint32 n) (uint16 k))
    (dotimes (i n)
      (let* ((x (mod (read-fixnum) (* 2 k)))
             (y (mod (read-fixnum) (* 2 k)))
             (c (fast-read-char))
             (table (if (char= c #\B) btable wtable)))
        (incf (aref table y x))
        (when (< (+ x (* 2 k)) (* 3 k))
          (incf (aref table y (+ x (* 2 k))))
          (when (< (+ y (* 2 k)) (* 3 k))
            (incf (aref table (+ y (* 2 k)) (+ x (* 2 k))))))
        (when (< (+ y (* 2 k)) (* 3 k))
          (incf (aref table (+ y (* 2 k)) x)))))
    (dotimes (x (+ 1 (* 3 k)))
      (dotimes (y (* 3 k))
        (incf (aref btable (+ y 1) x) (aref btable y x))
        (incf (aref wtable (+ y 1) x) (aref wtable y x))))
    (dotimes (y (+ 1 (* 3 k)))
      (dotimes (x (* 3 k))
        (incf (aref btable y (+ x 1)) (aref btable y x))
        (incf (aref wtable y (+ x 1)) (aref wtable y x))))
    (let ((res 0))
      (declare (uint32 res))
      (dotimes (x k)
        (dotimes (y k)
          (let ((value1 (+ (get-2dcumul btable y x (+ y k) (+ x k))
                           (get-2dcumul btable (+ y k) (+ x k) (+ y k k) (+ x k k))
                           (get-2dcumul wtable y (+ x k) (+ y k) (+ x k k))
                           (get-2dcumul wtable (+ y k) x (+ y k k) (+ x k))))
                (value2 (+ (get-2dcumul wtable y x (+ y k) (+ x k))
                           (get-2dcumul wtable (+ y k) (+ x k) (+ y k k) (+ x k k))
                           (get-2dcumul btable y (+ x k) (+ y k) (+ x k k))
                           (get-2dcumul btable (+ y k) x (+ y k k) (+ x k)))))
            (setf res (max res value1 value2)))))
      (println res))))

#-swank(main)


Measured memory_bytes: 104936