#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;
typedef pair<int, int> coord;
const long max_size=1005*2;
vector<vector<long>> white_bd(max_size, vector<long>(max_size,0));
vector<vector<long>> blk_bd(max_size, vector<long>(max_size,0));

vector<vector<long>> i_white(max_size,vector<long>(max_size,0));
vector<vector<long>> i_blk(max_size,vector<long>(max_size,0));
long n,k;

int main(){
    long count=0;
    char c;
    cin >> n >> k;
    for(int i=0; i<n; i++){
        long x,y;
        char cl;
        cin >> x >> y >> cl;
        x = x%(k*2);
        y = y%(k*2);
        if( cl == 'B' ) blk_bd[y][x]++;
        else white_bd[y][x]++;
    }

    i_white[0][0] = white_bd[0][0];
    i_blk[0][0] = blk_bd[0][0];
    for(int i=0; i<(2*k); i++){
        if( i!=0){
            i_white[i][0] = i_white[i-1][0]+white_bd[i][0];
            i_blk[i][0] = i_blk[i-1][0]+blk_bd[i][0];
        }
        for(int j=1; j<(2*k); j++){
            if(i==0) {
                i_white[0][j] = i_white[0][j-1]+white_bd[0][j];
                i_blk[0][j] = i_blk[0][j-1]+blk_bd[0][j];
            }else{
               i_white[i][j] = i_white[i-1][j]+i_white[i][j-1]-i_white[i-1][j-1]+white_bd[i][j]; 
               i_blk[i][j] = i_blk[i-1][j]+i_blk[i][j-1]-i_blk[i-1][j-1]+blk_bd[i][j]; 
            }
        }
    }
    /*
    for(int i=(2*k-1);i>=0;i--){
        for(int j=0;j<2*k;j++){
            cout << i_white[i][j] << " ";
        }
        cout << endl;
    }
    for(int i=(2*k-1);i>=0;i--){
        for(int j=0;j<2*k;j++){
            cout << i_blk[i][j] << " ";
        }
        cout << endl;
    }
    */
    long max = 0, rev_max=0;
    for(int i=0; i<k; i++){
        for(int j=0; j<k; j++){
            long count=0, rev_count=0;
            long sec[9]={0}, rev_sec[9]={0};
            if( (i>0)&&(j>0) ){
                sec[0] = i_blk[i-1][j-1];
                rev_sec[0] = i_white[i-1][j-1];
            }else{
                sec[0] = 0;
                rev_sec[0] = 0;
            }

            if( i>0 ){
                sec[1] = i_blk[i-1][j+k-1] - sec[0];
                sec[2] = i_blk[i-1][2*k-1] - sec[0] -sec[1];

                rev_sec[1] = i_white[i-1][j+k-1] - rev_sec[0];
                rev_sec[2] = i_white[i-1][2*k-1] - rev_sec[0] - rev_sec[1];
            }else{
                sec[1] = 0; sec[2] = 0;
                rev_sec[1] = 0; rev_sec[2] = 0;
            }

            if( j>0 ){
                sec[3] = i_blk[i+k-1][j-1]  - sec[0];
                sec[6] = i_blk[2*k-1][j-1] - sec[0] - sec[3];

                rev_sec[3] = i_white[i+k-1][j-1]  - rev_sec[0];
                rev_sec[6] = i_white[2*k-1][j-1] - rev_sec[0] - rev_sec[3];
            }else{
                sec[3] = 0; sec[6] = 0;
                rev_sec[3] = 0; rev_sec[6] = 0;
            }

            sec[4] = i_blk[i+k-1][j+k-1] - sec[1] - sec[3] - sec[0];
            sec[5] = i_blk[i+k-1][2*k-1] - sec[4] -sec[3] - sec[2] - sec[1] - sec[0];
            sec[7] = i_blk[2*k-1][j+k-1] - sec[6] - sec[4] - sec[3] - sec[1] - sec[0];
            sec[8] = i_blk[2*k-1][2*k-1];
            for(int s=0; s<8; s++) sec[8] -= sec[s];

            rev_sec[4] = i_white[i+k-1][j+k-1] - rev_sec[1] - rev_sec[3] - rev_sec[0];
            rev_sec[5] = i_white[i+k-1][2*k-1] - rev_sec[4] - rev_sec[3] - rev_sec[2] - rev_sec[1] - rev_sec[0];
            rev_sec[7] = i_white[2*k-1][j+k-1] - rev_sec[6] - rev_sec[4] - rev_sec[3] - rev_sec[1] - rev_sec[0];
            rev_sec[8] = i_white[2*k-1][2*k-1];
            for(int s=0; s<8; s++) rev_sec[8] -= rev_sec[s];

            count = sec[0]+rev_sec[1]+sec[2]+rev_sec[3]+sec[4]+rev_sec[5]+
                sec[6]+rev_sec[7]+sec[8];

            rev_count = rev_sec[0]+sec[1]+rev_sec[2]+sec[3]+rev_sec[4]+sec[5]+
                rev_sec[6]+sec[7]+rev_sec[8];

            if( count > max ) max = count;
            if( rev_count > max ) max = rev_count;
            /*
            cout << i << ", " << j << endl;
            for(int s=0; s<3; s++){
                for(int t=0; t<3; t++){
                    cout << sec[3*s+t]    << " ";
                }
                cout << endl;
            }
            for(int s=0; s<3; s++){
                for(int t=0; t<3; t++){
                    cout << rev_sec[3*s+t]    << " ";
                }
                cout << endl;
            }
            */
        }
    }


    cout << max << endl;
    return 0;
}


Measured memory_bytes: 126848