Word Game 具体求解

Problem Description
If you have a circle consists of L characters. We can define one of them as 0th character, and then 1st, 2nd …… (L -1)th along the clockwise. A string beginning with the ith character can be get from this circle, let's call it pi. From 0 to L -1, if there exists exactly K positions i make p[i] as same as p[0], then we call it a magic word.
Give you n strings and a K. Please use all of these strings to creat a new string p. Tell me how many ways exists to construct a string p and make it a magic word.

Input
The input contains several cases. Each case starts with two integer n and K. Then follows n lines, each line contain a string. 0 < n <= 8, 0 < K <= 200, each string contain between 1 and 20 characters. Input ends with a case n = 0 and K = 0.

Output
Each case print a line with an integer.

Sample Input
3 3
AAB
AAB
AAB
0 0

Sample Output
6