fn get_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().to_string()
}

fn reads<T>() -> Vec<T>
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug {
    get_line().split(' ').map(|x| x.parse().unwrap()).collect()
}

fn main() {
    let nk = reads();

    let k = nk[1];

    let mut xs = vec![vec![0; 2 * k]; 2 * k];

    for _ in 0..nk[0] {
        let xyc: Vec<usize> = get_line().split(' ').map(|x| {
            if x == "W" {
                1
            }
            else if x == "B" {
                0
            }
            else {
                x.parse().unwrap()
            }
        }).collect();

        let x = xyc[0] % (2 * k);
        let y = xyc[1] % (2 * k);
        let m = xyc[2] == 1;

        if m {
            xs[x][y] += 1;
        }
        else {
            let x = (x + k) % (2 * k);
            xs[x][y] += 1;
        }
    }

    for i in 1..(2 * k) {
        xs[i][0] += xs[i - 1][0];
        xs[0][i] += xs[0][i - 1];
    }

    for i in 1..(2 * k) {
        for j in 1..(2 * k) {
            xs[i][j] += xs[i - 1][j] + xs[i][j - 1] - xs[i - 1][j - 1];
        }
    }

    let k2 = 2 * k - 1;

    let mut res = -1;

    use std::cmp::max;

    for i in 0..k {
        for j in 0..k {
            if i == k - 1 && i == j {
                let x0 = xs[i][j];
                let x1 = xs[k2][j] - x0;
                let x2 = xs[i][k2] - x0;
                let x3 = xs[k2][k2] - x0 - x1 - x2;

                let v0 = x0 + x3;
                let v1 = x1 + x2;

                res = max(res, max(v0, v1));
            }
            else if i == k - 1 {
                let x0 = xs[i][j];
                let x1 = xs[k2][j] - x0;
                let x2 = xs[i][j + k] - x0;
                let x3 = xs[k2][j + k] - x0 - x1 - x2;
                let x4 = xs[i][k2] - x0 - x2;
                let x5 = xs[k2][k2] - x0 - x1 - x2 - x3 - x4;

                let v0 = x0 + x3 + x4;
                let v1 = x1 + x2 + x5;

                res = max(res, max(v0, v1));
            }
            else if j == k - 1 {
                let x0 = xs[i][j];
                let x1 = xs[i + k][j] - x0;
                let x2 = xs[k2][j] - x0 - x1;
                let x3 = xs[i][k2] - x0;
                let x4 = xs[i + k][k2] - x0 - x1 - x3;
                let x5 = xs[k2][k2] - x0 - x1 - x2 - x3 - x4;

                let v0 = x0 + x2 + x4;
                let v1 = x1 + x3 + x5;

                res = max(res, max(v0, v1));
            }
            else {
                let x0 = xs[i][j];
                let x1 = xs[i + k][j] - x0;
                let x2 = xs[k2][j] - x0 - x1;
                let x3 = xs[i][j + k] - x0;
                let x4 = xs[i + k][j + k] - x0 - x1 - x3;
                let x5 = xs[k2][j + k] - x0 - x1 - x2 - x3 - x4;
                let x6 = xs[i][k2] - x0 - x3;
                let x7 = xs[i + k][k2] - x0 - x1 - x3 - x4 - x6;
                let x8 = xs[k2][k2] - x0 - x1 - x2 - x3 - x4 - x5 - x6 - x7;

                let v0 = x0 + x2 + x4 + x6 + x8;
                let v1 = x1 + x3 + x5 + x7;
                
                res = max(res, max(v0, v1));
            }
        }
    }

    println!("{}", res);
}

Measured memory_bytes: 20732