Each cell of an R×C grid holds one lowercase letter. Start on any cell and make L−1 moves, each one onto a cell that touches the current cell on the left, above, to the right, or below. The letters of the visited cells, read in visit order, spell a word of length L.
Two rules govern a move. You cannot leave the grid. You may step onto a cell you have already visited, including the cell you just came from. When L is 1 there is no move, so the letter in a single cell is already a word.
Build every word this way, over every starting cell and every sequence of moves. The letters a, c, and m are forbidden. Any word that contains at least one forbidden letter is thrown away. For example, ici, ahi, and mrk are all thrown away.
Count how many words survive, and how many distinct strings appear among them. A different starting cell or a different route counts as a separate word even when it spells the same string, but the distinct count charges that string only once.
The first line contains the number of rows R, the number of columns C, and the word length L, separated by spaces. (0<R<11, 0<C<11, 0<L<7)
Each of the next R lines holds one row of the grid: C lowercase letters with no spaces.
Print the number of words of length L on the first line. Print how many of them are distinct on the second line.