#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

#define MAX_N (100000)
#define MAX_K (1000)

using namespace std;

// nwss[i][j]: The number of (xs[i], ys[i], cs[i]) where
//                 xs[i] % (2 * K) == i
//                 ys[i] % (2 * K) == j
//                 cs[i] == 'W'
// nbss[i][j]: The same as wss except for 'B'.
int nwss[2 * MAX_K][2 * MAX_K] = {}, nbss[2 * MAX_K][2 * MAX_K] = {};

// acc_nwss[i][j]: The number of (xs[i], ys[i], cs[i]) where
//                     xs[i] % (2 * K) <= i
//                     ys[i] % (2 * K) <= j
//                     cs[i] == 'W'
// acc_nbss[i][j]: The same as acc_nwss except for 'B'.
int acc_nwss[2 * MAX_K][2 * MAX_K] = {}, acc_nbss[2 * MAX_K][2 * MAX_K] = {};

int main(int argc, char *argv[]) {
  // read inputs
  int N, K, xs[MAX_N], ys[MAX_N];
  char cs[MAX_N];
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d %d %c", &xs[i], &ys[i], &cs[i]);
    // printf("%d %d %c\n", xs[i], ys[i], cs[i]);
  }

  // calculate nwss, nbss
  for (int i = 0; i < N; i++) {
    const int xd = xs[i] % (2 * K), yd = ys[i] % (2 * K);
    if (cs[i] == 'W') {
      nwss[xd][yd]++;
    } else {
      nbss[xd][yd]++;
    }
  }

  // calculate acc_nwss, acc_nbss
  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      acc_nwss[i][j] = (j - 1 >= 0 ? acc_nwss[i][j - 1] : 0) +
                       (i - 1 >= 0 ? acc_nwss[i - 1][j] : 0) -
                       (i - 1 >= 0 && j - 1 >= 0 ? acc_nwss[i - 1][j - 1] : 0) +
                       nwss[i][j];
      acc_nbss[i][j] = (j - 1 >= 0 ? acc_nbss[i][j - 1] : 0) +
                       (i - 1 >= 0 ? acc_nbss[i - 1][j] : 0) -
                       (i - 1 >= 0 && j - 1 >= 0 ? acc_nbss[i - 1][j - 1] : 0) +
                       nbss[i][j];
    }
  }
  // for (int i = 0; i < 2 * K; i++) {
  //   for (int j = 0; j < 2 * K; j++) {
  //     printf("%d ", acc_nbss[i][j]);
  //   }
  //   printf("\n");
  // }

  // solve
  int ans = 0;
  for (int a = 1; a <= 2 * K; a++) {
    for (int b = 1; b <= 2 * K; b++) {
      // calculate number of wishes granted for checker board
      // characterized by (a, b).
      const int b1 = a > 0 && a <= K, b2 = b > 0 && b <= K;
      const int pattern = b1 == b2;
      const int(*acc_n1ss)[2 * MAX_K] = pattern ? acc_nbss : acc_nwss;
      const int(*acc_n2ss)[2 * MAX_K] = pattern ? acc_nwss : acc_nbss;
      const int ad = b1 ? a : a - K, bd = b2 ? b : b - K;
      const int add = ad + K, bdd = bd + K;
      const int num_granted =
          (acc_n1ss[add - 1][bdd - 1] - acc_n1ss[ad - 1][bdd - 1] -
           acc_n1ss[add - 1][bd - 1] + acc_n1ss[ad - 1][bd - 1]) +
          (acc_n2ss[ad - 1][bdd - 1] - acc_n2ss[ad - 1][bd - 1]) +
          (acc_n2ss[add - 1][bd - 1] - acc_n2ss[ad - 1][bd - 1]) +
          (acc_n2ss[2 * K - 1][bdd - 1] - acc_n2ss[2 * K - 1][bd - 1] -
           acc_n2ss[add - 1][bdd - 1] + acc_n2ss[add - 1][bd - 1]) +
          (acc_n2ss[add - 1][2 * K - 1] - acc_n2ss[add - 1][bdd - 1] -
           acc_n2ss[ad - 1][2 * K - 1] + acc_n2ss[ad - 1][bdd - 1]) +
          (acc_n1ss[2 * K - 1][2 * K - 1] - acc_n1ss[2 * K - 1][bdd - 1] -
           acc_n1ss[add - 1][2 * K - 1] + acc_n1ss[add - 1][bdd - 1]) +
          (acc_n1ss[ad - 1][2 * K - 1] - acc_n1ss[ad - 1][bdd - 1]) +
          (acc_n1ss[2 * K - 1][bd - 1] - acc_n1ss[add - 1][bd - 1]) +
          acc_n1ss[ad - 1][bd - 1];
      ans = max(ans, num_granted);
    }
  }
  printf("%d\n", ans);

  return 0;
}


Measured memory_bytes: 63488