Under construction

Still being built. Things may change

Honey Tour

Time limit1sMemory limit256 MB

Summary
For each top entrance and bottom exit of K stacked copies of an N by M grid, find the maximum honey pots on a simple path and how many paths reach it.
Level

Hard9 of 10

Topics
Dynamic programming, Bit manipulation, Graph, Matrix
Solved
No attempts yet

Problem

A little bear Koma likes honey very much. One day, Koma found a storehouse full of honey. The storehouse has MM entrances and MM exits, and its layout is as complicated as a maze. Soon Koma understood the structure and drew a map of it. The map is a two-dimensional grid of N⋅KN \cdot K rows and MM columns. Each square contains either an obstacle or a honey pot. The map has one special property: it consists of KK identical copies of the same N×MN \times M grid, stacked one below the other.

The top side of each of the MM columns is an entrance, and the bottom side of each column is an exit. Koma takes a tour through the storehouse under these rules:

  1. Koma enters through one of the entrances and leaves through one of the exits.
  2. Koma can move from one square to another if the two squares share a side.
  3. Koma can occupy a square only if the square contains no obstacle.
  4. Koma can visit each square at most once.
  5. Koma can eat from each honey pot visited.

Koma wants to visit as many honey pots as possible. For each entrance ii and each exit jj, compute two integers: the maximum number of honey pots Koma can visit when entering through the ii-th entrance and leaving through the jj-th exit, and the number of tours that reach this maximum. The number of tours can be large, so print it modulo 109+710^{9} + 7.

Input

The first line contains three integers NN, MM, and KK (1≤N≤51 \le N \le 5, 2≤M≤72 \le M \le 7, 2≤N⋅M≤252 \le N \cdot M \le 25, 1≤K≤1091 \le K \le 10^{9}).

The next NN lines each contain MM characters that describe the repeating pattern. A '.' marks a square with a honey pot, and an 'X' marks a square with an obstacle.

Output

Print two M×MM \times M matrices, one after another. The first matrix holds the maximum number of honey pots Koma can visit. The second matrix holds the number of tours that reach that maximum, modulo 109+710^{9} + 7. The jj-th number on the ii-th line corresponds to entering through the ii-th entrance and leaving through the jj-th exit.

Examples1

  1. Example 1

    Input
    2 2 3
    ..
    .X
    
    Expected output
    6 0
    7 0
    1 0
    1 0