using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;
using TextReader = System.IO.TextReader;
using System.Text;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            K = consoleReader.Int;
            var K2 = 2 * K;
            var white = new int[K2, K2];
            var black = new int[K2, K2];
            foreach (var cr in consoleReader.Repeat(N))
            {
                var x = cr.Int % K2;
                var y = cr.Int % K2;
                if (cr.String == "W")
                    white[x, y]++;
                else
                    black[x, y]++;
            }
            whiteSum = new int[K2, K2];
            whiteSum[0, 0] = white[0, 0];
            blackSum = new int[K2, K2];
            blackSum[0, 0] = black[0, 0];
            for (int i = 1; i < K2; i++)
            {
                whiteSum[i, 0] = whiteSum[i - 1, 0] + white[i, 0];
                blackSum[i, 0] = blackSum[i - 1, 0] + black[i, 0];
                whiteSum[0, i] = whiteSum[0, i - 1] + white[0, i];
                blackSum[0, i] = blackSum[0, i - 1] + black[0, i];
            }
            for (int i = 1; i < K2; i++)
                for (int j = 1; j < K2; j++)
                {
                    whiteSum[i, j] = whiteSum[i, j - 1] + whiteSum[i - 1, j] - whiteSum[i - 1, j - 1] + white[i, j];
                    blackSum[i, j] = blackSum[i, j - 1] + blackSum[i - 1, j] - blackSum[i - 1, j - 1] + black[i, j];
                }

            var max = 0;
            for (int i = 0; i < K; i++)
                for (int j = 0; j < 2 * K; j++)
                {
                    var whiteCount = 0;
                    var blackCount = 0;
                    for (int x = -2; x <= 2; x++)
                        for (int y = -2; y <= 2; y++)
                        {
                            if (((x + y) & 1) == 0)
                                whiteCount += Count(whiteSum, i + x * K, j + y * K);
                            else
                                blackCount += Count(blackSum, i + x * K, j + y * K);
                        }
                    max = Math.Max(max, whiteCount + blackCount);
                }

            return max;
        }
        int Count(int[,] sums, int startX, int startY)
        {
            var endXexclusive = Math.Min(2 * K, startX + K);
            var endYexclusive = Math.Min(2 * K, startY + K);
            startX = Math.Max(0, startX);
            startY = Math.Max(0, startY);

            if (startX >= 2 * K || startY >= 2 * K || endXexclusive <= 0 || endYexclusive <= 0)
                return 0;

            if (startX == 0 && startY == 0)
                return sums[endXexclusive - 1, endYexclusive - 1];
            if (startX == 0)
                return sums[endXexclusive - 1, endYexclusive - 1] - sums[endXexclusive - 1, startY - 1];
            if (startY == 0)
                return sums[endXexclusive - 1, endYexclusive - 1] - sums[startX - 1, endYexclusive - 1];
            return sums[endXexclusive - 1, endYexclusive - 1] - sums[endXexclusive - 1, startY - 1] - sums[startX - 1, endYexclusive - 1] + sums[startX - 1, startY - 1];
        }
        int K;
        int[,] whiteSum;
        int[,] blackSum;

        #region いつもの
#pragma warning disable
        private ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader(Console.In)).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
    }
    public class ConsoleReader
    {
        private string[] ReadLineSplit() => textReader.ReadLine().Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
        private string[] line = Array.Empty<string>();
        private int linePosition;
        private TextReader textReader;
        public ConsoleReader(TextReader tr) { textReader = tr; }
        public int Int => int.Parse(String);
        public long Long => long.Parse(String);
        public double Double => double.Parse(String);
        public string String
        {
            get
            {
                if (linePosition >= line.Length)
                {
                    linePosition = 0;
                    line = ReadLineSplit();
                }
                return line[linePosition++];
            }
        }
        public class SplitLine
        {
            private string[] splited;
            public SplitLine(ConsoleReader cr) { splited = cr.ReadLineSplit(); cr.line = Array.Empty<string>(); }
            public int[] Int => String.Select(x => int.Parse(x)).ToArray();
            public int[] Int0 => String.Select(x => int.Parse(x) - 1).ToArray();
            public long[] Long => String.Select(x => long.Parse(x)).ToArray();
            public double[] Double => String.Select(x => double.Parse(x)).ToArray();
            public string[] String => splited;
        }
        public SplitLine Split => new SplitLine(this);
        public class RepeatReader : IEnumerable<ConsoleReader>
        {
            ConsoleReader cr; int count;
            public RepeatReader(ConsoleReader cr, int count) { this.cr = cr; this.count = count; }
            public IEnumerator<ConsoleReader> GetEnumerator() => Enumerable.Repeat(cr, count).GetEnumerator();
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();

            public IEnumerable<string> String => this.Select(cr => cr.String);
            public IEnumerable<int> Int => this.Select(cr => cr.Int);
            public IEnumerable<int> Int0 => this.Select(cr => cr.Int - 1);
            public IEnumerable<long> Long => this.Select(cr => cr.Long);
            public IEnumerable<double> Double => this.Select(cr => cr.Double);
        }
        public RepeatReader Repeat(int count) => new RepeatReader(this, count);
    }
    #endregion
}

Measured memory_bytes: 75580