#include<iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int n, k, result[100], i, p, a, b, c, d, e, x;
	char card[100][3];
	char number[10000][9];
	int number2[10000];

	for (i = 0;; i++)
	{
		cin >> n;
		cin >> k;

		a = 0;
		b = 0;
		c = 0;
		d = 0;
		e = 0;
		p = 0;
		x = 0;

		if (n == 0 && k == 0)
		{
			break;
		}

		for (int a = 0; a < n; a++)
		{
			cin >> card[a];
		}

		if (k == 2)
		{
			result[i] = n*(n - 1);
			for (a; a < n*n-x; a++)
			{
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						x++;
						a--;
					}
				}
				c++;
			}

			for (int a = 0; a < n*n - x; a++)
			{
				number2[a] = atoi(number[a]);
			}

			for (int a = 0; a < n*n - 1-x; a++)
			{
				if (number2[a] != 0)
				{
					for (int b = a + 1; b < n*n-x; b++)
					{
						if (number2[a] == number2[b])
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}

		else if (k == 3)
		{
			result[i] = n*(n - 1)*(n - 2);
			for (int a = 0; a < n*n*n-x; a++)
			{
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && c != d && d != b)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						a--;
						x++;
					}
				}
				d++;
			}

			for (int a = 0; a < n*n*n - x; a++)
			{
				number2[a] = atoi(number[a]);
			}

			for (int a = 0; a < n*n*n - 1-x; a++)
			{
				if (number2[a] != 0)
				{
					for (int b = a + 1; b < n*n*n-x; b++)
					{
						if (number2[a] == number2[b])
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}

		else if (k == 4)
		{
			result[i] = n*(n - 1)*(n - 2)*(n - 3);
			for (int a = 0; a < n*n*n*n-x; a++)
			{
				if (e >= n)
				{
					e = 0;
					d++;
				}
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && b != d && b != e && c != d && c != e && d != e)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[e][f] != *"")
							{
								number[a][p] = card[e][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						a--;
						x++;
					}
				}
				e++;
			}
		}
		
		for (int a = 0; a < n*n*n*n - x; a++)
		{
			number2[a] = atoi(number[a]);
		}

		for (int a = 0; a < n*n*n*n - 1-x; a++)
		{
			if (number2[a] != 0)
			{
				for (int b = a + 1; b < n*n*n*n-x; b++)
				{
					if (number2[a] == number2[b])
					{
						number[a][0] = *"0";
						number[a][1] = *"";
						result[i]--;
						break;
					}
				}
			}
		}
	}

	for (int a = 0; a < i; a++)
	{
		cout << result[a] << endl;
	}

	return 0;
}

Measured memory_bytes: 1268