#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

fn solve() {
    let (n,k) = get!(usize, usize);
    let xyc = get!(usize, usize, String; n);

    let (white, black): (Vec<_>, Vec<_>) = xyc.into_iter()
        .map(|(x,y,c)| (x,y,c.chars().next().unwrap()=='W'))
        .map(|(x,y,c)| (x%(2*k),y%(2*k),c))
        .map(|(x,y,c)| if x>=k && y>=k {
            (x-k,y-k,c)
        } else if x<k && y>=k {
            (x+k,y-k,c)
        } else {
            (x,y,c)
        })
        .partition(|&(x,y,c)| x<k);
    let black = black.into_iter().map(|(x,y,c)| (x-k,y,c)).collect::<Vec<_>>();

    let mut wa = vec![vec![None; k];k];
    let mut ba = vec![vec![None; k];k];
    for (x,y,c) in white {
        if wa[x][y].is_none() {
            wa[x][y] = Some((0,0));
        }
        if let Some(v) = wa[x][y].as_mut() {
            if c {
                (*v).0 += 1;
            } else {
                (*v).1 += 1;
            }
        }
    }
    for (x,y,c) in black {
        if ba[x][y].is_none() {
            ba[x][y] = Some((0,0));
        }
        if let Some(v) = ba[x][y].as_mut() {
            if c {
                (*v).0 += 1;
            } else {
                (*v).1 += 1;
            }
        }
    }
    let mut wsums = vec![vec![vec![0;2];k+1];k+1];
    let mut bsums = vec![vec![vec![0;2];k+1];k+1];
    for i in 0..k {
        for j in 0..k {
            if let Some((w,b)) = wa[i][j] {
                wsums[i+1][j+1][1] = wsums[i+1][j][1]+w;
                wsums[i+1][j+1][0] = wsums[i+1][j][0]+b;
            } else {
                for l in 0..2 {
                    wsums[i+1][j+1][l] = wsums[i+1][j][l];
                }
            }
            if let Some((w,b)) = ba[i][j] {
                bsums[i+1][j+1][1] = bsums[i+1][j][1]+b;
                bsums[i+1][j+1][0] = bsums[i+1][j][0]+w;
            } else {
                for l in 0..2 {
                    bsums[i+1][j+1][l] = bsums[i+1][j][l];
                }
            }
        }
        for j in 0..k+1 {
            for l in 0..2 {
                wsums[i+1][j][l] += wsums[i][j][l];
                bsums[i+1][j][l] += bsums[i][j][l];
            }
        }
    }

    let mut mx = 0;
    for i in 0..k+1 {
        for j in 0..k+1 {
            let wval = wsums[k][k][1]+2*wsums[i][j][1]-wsums[i][k][1]-wsums[k][j][1]
                +wsums[i][k][0]+wsums[k][j][0]-2*wsums[i][j][0];
            let bval = bsums[k][k][1]+2*bsums[i][j][1]-bsums[i][k][1]-bsums[k][j][1]
                +bsums[i][k][0]+bsums[k][j][0]-2*bsums[i][j][0];
            let val = wval+bval;
            mx = max!(mx,val,n-val);
        }
    }
    println!("{}", mx);
}


Measured memory_bytes: 153980