import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib, core.bitop;

struct Cell{
    long black, white;
    ref long opIndex(char c)
    {
        return c=='B'?black:white;
    }
}
void main()
{
    auto N = scanElem;
    auto K = scanElem;

    auto grid = new Grid!Cell(K*2,K);
    long countWhite;

    Data2 calcIndex(long x, long y){
        if(x<0) x = K-x;
        if(y<0) y = K-y;
        return Data2((x/K+y/K)%2 * K + x%K, y%K);
    }
    foreach(i; 0..N)
    {
        auto x = scanElem;
        auto y = scanElem;
        auto c = scanElem!char;
        if(c=='W')countWhite++;
        
        grid[calcIndex(x, y)][c]++;
    }

    auto sumGrid = new Grid!Cell(K*3,K*3);
    foreach(y; 0..K*3)
    {
        foreach(x; 0..K*3)
        {
            sumGrid[x, y] = grid[calcIndex(x,y)];
            if(x!=0){
                sumGrid[x, y].black += sumGrid[x-1, y].black;
                sumGrid[x, y].white += sumGrid[x-1, y].white;
            }
            if(y!=0){
                sumGrid[x, y].black += sumGrid[x, y-1].black;
                sumGrid[x, y].white += sumGrid[x, y-1].white;
            }
            if(x!=0&&y!=0){
                sumGrid[x, y].black -= sumGrid[x-1, y-1].black;
                sumGrid[x, y].white -= sumGrid[x-1, y-1].white;
            }
        }
    }
    long res;
    foreach(y; K-1..K*3)
    {
        foreach(x; K-1..K*3)
        {
            auto b = sumGrid[x, y].black;
            auto w = sumGrid[x, y].white;
            if(x-K>=0){
                b -= sumGrid[x-K, y].black;
                w -= sumGrid[x-K, y].white;
            }

            if(y-K>=0)
            {
                b -= sumGrid[x, y-K].black;
                w -= sumGrid[x, y-K].white;
            }

            if(x-K>=0&&y-K>=0)
            {
                b += sumGrid[x-K, y-K].black;
                w += sumGrid[x-K, y-K].white;
            }
            res = max(res, b+(countWhite-w));
            // if(b+(countWhite-w)==3)
            // {
            //     print("b: ", b);
            //     print(" w: ", w);
            //     print(" x: ", x);
            //     print(" y: ", y);
            //     print("\n");
            // }
        }
    }
    // println(grid);
    // println(sumGrid);
    writeln(res);
}

enum MOD=pow(10,9)+7L;

class GridIter(T){
    private const T[] grid;
    size_t count;

    this(T[] g)
    {
        grid = g;
    }

    void popFront(){
        count++;
    }
    bool empty(){
        return count>=grid.length;
    }
    T front(){
        return grid[count];
    }
}
class Grid(T){
    T[] grid;
    size_t width, height;

    private this(){

    }

    this(size_t w, size_t h)
    {
        width = w;
        height = h;
        grid.length = width*height;
    }

    GridIter!T iter(){
        return new GridIter!T(grid);
    }

    ref T opIndex(size_t x, size_t y)
    {
        return grid[x+width*y];
    }
    ref T opIndex(E)(E e)
        if(
            isIntegral!(typeof(e.x)) &&
            isIntegral!(typeof(e.y)))
    {
        return this[e.x, e.y];
    }

    Grid!T opIndex(size_t[2] r1, size_t[2] r2)
    {
        auto res = new Grid!T();
        auto startOffset = r1[0] + r2[0]*width;
        auto endOffset = r1[1] + (r2[1] - 1)*width;
        res.grid = grid[startOffset .. endOffset];

        res.width = r1[1] - r1[0];
        res.height = r2[1] - r2[0];
        return res;
    }
    
    Grid!T opIndex(size_t[2] r1, size_t j) { return opIndex(r1, [j, j+1]); }
    Grid!T opIndex(size_t i, size_t[2] r2) { return opIndex([i, i+1], r2); }

    size_t[2] opSlice(size_t dim)(size_t start, size_t end)
    if (dim == 0 || dim == 1)
    in { assert(start >= 0 && end <= this.opDollar!dim); }
    body{
        return [start, end];
    }

    size_t opDollar(size_t dim : 0)() { return width; }
    size_t opDollar(size_t dim : 1)() { return height; }

    override string toString(){
        string res = "\n";
        foreach_reverse(line; 0..height)
        {
            res ~= grid[line*width..(line+1)*width].to!string ~ '\n';
        }
        return res;
    }
}

struct Node{
    long[] connect;
}
struct Data2{
    long x,y;
    alias a=x, b=y;
    ref long opIndex(size_t i)
    {
        assert(i==0||i==1);
        return i==0?x:y;
    }
}
struct Data3{
    long x,y,z;
    alias a=x, b=y, c=z;
    ref long opIndex(size_t i)
    {
        assert(i==0||i==1||i==2);
        return i==0?x:(i==1?y:z);
    }
}

long gcd(long a, long b)
{
  if(b == 0) return a;
  return gcd(b, a % b);
}

class UnionFind{
    UnionFind parent = null;

    void merge(UnionFind a)
    {
        if(same(a)) return;
        a.root.parent = this.root;
    }
    UnionFind root()
    {
        if(parent is null)return this;
        return parent = parent.root;
    }
    bool same(UnionFind a)
    {
        return this.root == a.root;
    }
}

string scanString()
{
    return scanElem!string;
}
void scanValues(TList...)(ref TList list)
{
    auto lit = readln.splitter;
    foreach (ref e; list)
    {
        e = lit.fornt.to!(typeof(e));
        lit.popFront;
    }
}

void print(T...)(T t)
{
    stderr.write(t);
}
void println(T...)(T t)
{
    stderr.writeln(t);
}

T[] scanArray(T = long)()
{
    return readln.split.to!(T[]);
}

void scanStructs(T)(ref T[] t, size_t n)
{
    t.length = n;
    foreach (ref e; t)
    {
        auto line = readln.split;
        foreach (i, ref v; e.tupleof)
        {
            v = line[i].to!(typeof(v));
        }
    }
}

long scanULong(){
    long x;
    while(true){
        const c = getchar;
        if(c<'0'||c>'9'){
            break;
        }
        x = x*10+c-'0';
    }
    return x;
}

T scanElem(T = long)()
{
    char[] res;
    int c = ' ';
    while (isWhite(c) && c != -1)
    {
        c = getchar;
    }
    while (!isWhite(c) && c != -1)
    {
        res ~= cast(char) c;
        c = getchar;
    }
    return res.strip.to!T;
}

template fold(fun...) if (fun.length >= 1)
{
    auto fold(R, S...)(R r, S seed)
    {
        static if (S.length < 2)
        {
            return reduce!fun(seed, r);
        }
        else
        {
            import std.typecons : tuple;

            return reduce!fun(tuple(seed), r);
        }
    }
}

template cumulativeFold(fun...)
if (fun.length >= 1)
{
    import std.meta : staticMap;
    private alias binfuns = staticMap!(binaryFun, fun);

    auto cumulativeFold(R)(R range)
    if (isInputRange!(Unqual!R))
    {
        return cumulativeFoldImpl(range);
    }

    auto cumulativeFold(R, S)(R range, S seed)
    if (isInputRange!(Unqual!R))
    {
        static if (fun.length == 1)
            return cumulativeFoldImpl(range, seed);
        else
            return cumulativeFoldImpl(range, seed.expand);
    }

    private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
    {
        import std.algorithm.internal : algoFormat;

        static assert(Args.length == 0 || Args.length == fun.length,
            algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
                Args.stringof, fun.length));

        static if (args.length)
            alias State = staticMap!(Unqual, Args);
        else
            alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);

        foreach (i, f; binfuns)
        {
            static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
                    { args[i] = f(args[i], e); }()),
                algoFormat("Incompatible function/seed/element: %s/%s/%s",
                    fullyQualifiedName!f, Args[i].stringof, E.stringof));
        }

        static struct Result
        {
        private:
            R source;
            State state;

            this(R range, ref Args args)
            {
                source = range;
                if (source.empty)
                    return;

                foreach (i, f; binfuns)
                {
                    static if (args.length)
                        state[i] = f(args[i], source.front);
                    else
                        state[i] = source.front;
                }
            }

        public:
            @property bool empty()
            {
                return source.empty;
            }

            @property auto front()
            {
                assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
                static if (fun.length > 1)
                {
                    import std.typecons : tuple;
                    return tuple(state);
                }
                else
                {
                    return state[0];
                }
            }

            void popFront()
            {
                assert(!empty, "Attempting to popFront an empty cumulativeFold.");
                source.popFront;

                if (source.empty)
                    return;

                foreach (i, f; binfuns)
                    state[i] = f(state[i], source.front);
            }

            static if (isForwardRange!R)
            {
                @property auto save()
                {
                    auto result = this;
                    result.source = source.save;
                    return result;
                }
            }

            static if (hasLength!R)
            {
                @property size_t length()
                {
                    return source.length;
                }
            }
        }

        return Result(range, args);
    }
}

struct Factor
{
    long n;
    long c;
}

//素因数分解
Factor[] factors(long n)
{
    Factor[] res;
    for (long i = 2; i ^^ 2 <= n; i++)
    {
        if (n % i != 0)
            continue;

        int c;
        while (n % i == 0)
        {
            n = n / i;
            c++;
        }
        res ~= Factor(i, c);
    }
    if (n != 1)
        res ~= Factor(n, 1);

    return res;
}
//約数をすべて列挙
long[] divisors(long n)
{
    long[] list;
    void func(Factor[] fs, long n)
    {
        if(fs.empty){
            list ~= n;
            return;
        }

        foreach(c; 0..fs[0].c+1)
        {
            func(fs[1..$], n * (fs[0].n ^^ c));
        }
    }

    func(factors(n), 1);
    sort(list);
    return list;
}
//nまでの素数のリスト
long[] primes(long n)
{
    if(n<2)return [];
    auto table = new long[n+1];
    long[] res;
    for(int i = 2;i<=n;i++)
    {
        if(table[i]==-1) continue;
        for(int a = i;a<table.length;a+=i)
        {
            table[a] = -1;
        }
        res ~= i;
    }
    return res;
}
//素数判定
bool isPrime(long n)
{
    if (n <= 1)
        return false;
    if (n == 2)
        return true;
    if (n % 2 == 0)
        return false;
    for (long i = 3; i ^^ 2 <= n; i += 2)
        if (n % i == 0)
            return false;
    return true;
}

Measured memory_bytes: 176380