#P7930. Counting Sets in Card Sequences

    ID: 21115 Type: Default 1000ms 256MiB

Counting Sets in Card Sequences

Counting Sets in Card Sequences

You are given n cards. Each card is represented by a sequence of length k consisting only of the digits 1, 2, and 3. A SET is defined as an unordered triplet of distinct cards \(S_i, S_j, S_k\) (with \(i < j < k\)) such that for every position \(x\) (where \(1 \le x \le k\)), the three digits at this position satisfy one of the following conditions:

  • All three digits are the same, i.e. \(S_i(x) = S_j(x) = S_k(x)\).
  • All three digits are pairwise different, i.e. \(S_i(x), S_j(x), S_k(x)\) are all distinct. In other words, \(S_i(x) \neq S_j(x), S_i(x) \neq S_k(x), S_j(x) \neq S_k(x)\).

Your task is to compute the total number of such distinct SETs that can be formed from the provided cards.

For example, consider the cards 1123, 1322, and 1221. At positions 1 and 3, the digits are the same across the three cards, and at positions 2 and 4, they are all different. Thus, these three cards form a valid SET.

Note: Use the LaTeX format for any mathematical formulas, as demonstrated above.

inputFormat

The first line of input contains two integers n and k where n is the number of cards and k is the length of each card's sequence.

Following this, there are n lines, each containing a sequence of length k composed only of the digits 1, 2, and 3.

outputFormat

Output a single integer representing the total number of valid SETs (triplets) that satisfy the conditions described above.

sample

3 4
1123
1322
1221
1