Sub main()

    Dim RD As String
    Dim SD() As String
    
    
    Dim K As Long
    Dim N As Long
    Dim KiboNum As Long
    Dim OkCount As Long
    Dim OkCounts() As Long
    
    Dim x As Long
    Dim y As Long
    Dim c As String
    
    Dim Kx As Long
    Dim Ky As Long
    Dim Kc As String
    
    Dim Modx As Long
    Dim Mody As Long
    
    
    RD = Console.Readline()
    SD = Split(RD, " ")
    N = SD(0): K = SD(1)
    ReDim OkCounts(K - 1, K - 1)
    
    For KiboNum = 1 To N
        RD = Console.Readline()
        SD = Split(RD, " ")
        x = SD(0): y = SD(1): c = SD(2)
        'Bの時
        Kc = "B"
        For Ky = 0 To K - 1
            For Kx = 0 To K - 1
                Modx = (x Mod (K * 2)) + Kx
                Mody = (y Mod (K * 2)) + Ky
                If c = Kc Then
                    If (0 <= Modx And Modx <= (K - 1) And _
                        0 <= Mody And Mody <= (K - 1)) Or _
                       (K <= Modx And Modx <= (K * 2 - 1) And _
                        K <= Mody And Mody <= (K * 2 - 1)) Then
                        OkCounts(Kx, Ky) = OkCounts(Kx, Ky) + 1
                    End If
                Else
                    If (K <= Modx And Modx <= (K * 2 - 1) And _
                        0 <= Mody And Mody <= (K - 1)) Or _
                       (0 <= Modx And Modx <= (K - 1) And _
                        K <= Mody And Mody <= (K * 2 - 1)) Then
                        OkCounts(Kx, Ky) = OkCounts(Kx, Ky) + 1
                    End If
                End If
                
                If OkCount < OkCounts(Kx, Ky) Then
                    OkCount = OkCounts(Kx, Ky)
                End If
            Next Kx
        Next Ky
        
        'Wの時
        Kc = "W"
        For Ky = 0 To K - 1
            For Kx = 0 To K - 1
                Modx = (x Mod (K * 2)) + Kx
                Mody = (y Mod (K * 2)) + Ky
                If c = Kc Then
                    If (0 <= Modx And Modx <= (K - 1) And _
                        0 <= Mody And Mody <= (K - 1)) Or _
                       (K <= Modx And Modx <= (K * 2 - 1) And _
                        K <= Mody And Mody <= (K * 2 - 1)) Then
                        OkCounts(Kx, Ky) = OkCounts(Kx, Ky) + 1
                    End If
                Else
                    If (K <= Modx And Modx <= (K * 2 - 1) And _
                        0 <= Mody And Mody <= (K - 1)) Or _
                       (0 <= Modx And Modx <= (K - 1) And _
                        K <= Mody And Mody <= (K * 2 - 1)) Then
'                        OkCounts(Kx, Ky) = OkCounts(Kx, Ky) + 1
                    End If
                End If
                If OkCount < OkCounts(Kx, Ky) Then
                    OkCount = OkCounts(Kx, Ky)
                End If
            Next Kx
        Next Ky

    Next
    
    Console.Writeline (OkCount)


End Sub

end Module