This page is still under construction.

Parts of this page are still being built. What you see may change.

School Graduation

Time limit2sMemory limit1024 MB

Summary
Given an N by M grid of class letters, find the maximum number of colors so that cells in a shared column or class share a color.
Level

Medium6 of 10

Topics
Union-find, Graph, Hash map, Implementation
Solved
No attempts yet

Problem

The school administration has run into a problem with the upcoming graduation ceremony, and they hope you can help them solve it. During the ceremony, the students will stand in NN rows with MM students in each row. The administration wants the ceremony to be as colorful as possible, so they will hand out hats in various colors to the students.

For the arrangement to look nice, every student in the same column must have the same hat color. So that nobody feels left out, every student in the same class must also have the same hat color. Each student's row and column are already decided, but not their hat color. The administration needs your help assigning hat colors to the students so that the ceremony is as colorful as possible.

Write a program that, given how the students will be arranged at the graduation ceremony, computes the maximum number of unique hat colors that can be assigned to the students.

Input

The first line contains three integers NN, MM (1≤N,M≤7001 \leq N, M \leq 700), and KK (1≤K≤261 \leq K \leq 26): the number of rows, the number of columns, and the number of classes.

The following NN lines each contain MM characters and describe how the students will be arranged at the graduation ceremony. The character in row ii, column jj is an uppercase letter between A and the KK-th letter of the alphabet: the class that the student in row ii, column jj belongs to. There is guaranteed to be at least one student from each class.

Output

Print one integer: the maximum number of unique hat colors that can be assigned to the students so that every student in the same column, and every student in the same class, has the same hat color.

Hint

In the first sample, the second column contains one student from class A and one from class B. Since both of these students must have the same hat color, all of class A must have the same color as all of class B. It follows that every student at the ceremony must have the same color, so the answer is 11.

In the second sample, class A and class B must have the same color, since the first column contains a student from each of these two classes. Class C, however, can be given a different hat color. The answer is then 22.

In the third sample, we can give each class its own color, since no two students from different classes appear in the same column. The answer is 33.

In the last sample, we can assign one color to all students from classes A, B, and C, and another color to all students from classes D and E. The answer is 22.

Examples4

  1. Example 1

    Input
    2 3 2
    AAB
    ABB
    
    Expected output
    1
    
  2. Example 2

    Input
    2 2 3
    AC
    BC
    
    Expected output
    2
    
  3. Example 3

    Input
    2 3 3
    ABC
    ABC
    
    Expected output
    3
    
  4. Example 4

    Input
    3 5 5
    ABECE
    BCDAE
    CADBD
    
    Expected output
    2