Under construction

Still being built. Things may change

Travel of Alphabets

Interview

Time limit2sMemory limit256 MB

Summary
Count all length-L walks on a letter grid and the distinct strings among them, discarding any word containing a, c, or m.
Level

Easy3 of 10

Topics
Backtracking, DFS, Hash map
Solved
No attempts yet

Problem

Each cell of an R×CR \times C grid holds one lowercase letter. Start on any cell and make L−1L-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 LL.

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 LL 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.

Input

The first line contains the number of rows RR, the number of columns CC, and the word length LL, separated by spaces. (0<R<110 < R < 11, 0<C<110 < C < 11, 0<L<70 < L < 7)

Each of the next RR lines holds one row of the grid: CC lowercase letters with no spaces.

Output

Print the number of words of length LL on the first line. Print how many of them are distinct on the second line.

Examples3

  1. Example 1

    Input
    3 3 1
    abc
    def
    hij
    
    Expected output
    7
    7
    
  2. Example 2

    Input
    3 4 2
    acip
    mpbk
    pioc
    
    Expected output
    18
    14
    
  3. Example 3

    Input
    4 4 3
    abox
    btoy
    gitm
    ceui
    
    Expected output
    100
    73