unittest
{
	assert( [ "4 3", "0 1 W", "1 2 W", "5 3 B", "5 4 B" ].parse.expand.solve == 4 );
	assert( [ "2 1000", "0 0 B", "0 1 W" ].parse.expand.solve == 2 );
	assert( [ "6 2", "1 2 B", "2 1 W", "2 2 B", "1 0 B", "0 6 W", "4 5 W" ].parse.expand.solve == 4 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nk = input.front.split.to!( long[] );
	auto xycs = new Tuple!( long, long, dchar )[ nk[ 0 ] ];
	foreach( ref xyc; xycs )
	{
		input.popFront;
		auto ts = input.front.split.to!( dchar[][] );
		xyc = tuple( ts[ 0 ].to!( long ), ts[ 1 ].to!( long ), ts[ 2 ].front );
	}
	return tuple( nk[ 1 ], xycs );
}

auto solve( long k, Tuple!( long, long, dchar )[] xycs )
{
	auto kk = 2 * k;
	
	// 各希望を市松模様4セットの中にプロット
	auto bss = new long[][]( 1 + kk * 2, 1 + kk * 2 );
	auto wss = new long[][]( 1 + kk * 2, 1 + kk * 2 );
	foreach( xyc; xycs )
	{
		auto x = xyc[ 0 ] % kk;
		auto y = xyc[ 1 ] % kk;
		if( xyc[ 2 ] == 'B' )
		{
			bss[ 1 + x      ][ 1 + y      ]++;
			bss[ 1 + x + kk ][ 1 + y      ]++;
			bss[ 1 + x      ][ 1 + y + kk ]++;
			bss[ 1 + x + kk ][ 1 + y + kk ]++;
		}
		else
		{
			wss[ 1 + x      ][ 1 + y      ]++;
			wss[ 1 + x + kk ][ 1 + y      ]++;
			wss[ 1 + x      ][ 1 + y + kk ]++;
			wss[ 1 + x + kk ][ 1 + y + kk ]++;
		}
	}
	
	// 二次元累積和表に変換
	for( auto x = 1L; x < bss.length; x++ )
	{
		for( auto y = 1L; y < bss[ x ].length; y++ )
		{
			bss[ x ][ y ] += bss[ x - 1 ][ y ] + bss[ x ][ y - 1 ] - bss[ x - 1 ][ y - 1 ];
			wss[ x ][ y ] += wss[ x - 1 ][ y ] + wss[ x ][ y - 1 ] - wss[ x - 1 ][ y - 1 ];
		}
	}
	
	// 市松模様をずらして希望を叶えられる最大数を検索
	auto ans = 0L;
	for( auto x = kk; x < bss.length; x++ )
	{
		for( auto y = kk; y < bss[ x ].length; y++ )
		{
			auto bc = bss[ x      ][ y      ] - bss[ x - k  ][ y      ] - bss[ x      ][ y - k  ] + bss[ x - k  ][ y - k  ]
			        + bss[ x - k  ][ y - k  ] - bss[ x - kk ][ y - k  ] - bss[ x - k  ][ y - kk ] + bss[ x - kk ][ y - kk ];
			auto wc = wss[ x - k  ][ y      ] - wss[ x - kk ][ y      ] - wss[ x - k  ][ y - k  ] + wss[ x - kk ][ y - k  ]
			        + wss[ x      ][ y - k  ] - wss[ x      ][ y - kk ] - wss[ x - k  ][ y - k  ] + wss[ x - k  ][ y - kk ];
			ans = max( ans, bc + wc );
		}
	}
	return ans;
}
