The former king of Gridland -- CR -- has just passed away, leaving his K sons that all desire the throne. Then the war of succession breaks out and the K-King era begins.
Gridland is a rectangle of N*M, with each grid unit as a city. Each city supports one and only one of the K kings as its leader. Cities supporting the same king will always endeavor to establish a kingdom through fighting the rival, and they will not stop until they establish a kingdom satisfying the following rules:
1. The kingdom is a rectangle on the map.
2. Every city on the Gridland supporting that king should be inside the kingdom.
Sadly, through the process of establishing a kingdom, once the opposite cities find that an overlapping of domain is unavoidable, they will inevitably engage in a conflict.
Sometimes for a temporary benefit a bunch of kings may ally into a party. It is certain that one king can only take part in one party at one time, and the cities following a king will correspondingly support the party he joins.
Rather than fighting each other fiercely, cities supporting the same party cooperate and strive to establish a kingdom of the whole party.
Such kingdoms follow the similar rules:
1. The kingdom is a rectangle on the map.
2. Every city on the Gridland supporting that party should be inside the kingdom.
And the fighting trigger with other kingdoms is exactly the same as the above one.
A established kingdom is called stable if and only if:
1. No city in the kingdom has to fight with another kingdom..
2. If the kingdom stands for a party, the party must never have more than R kings.
Now, your job is to find the largest possible stable kingdom. By 'largest' we mean the one with the largest number of cities.