#P7450. Sharing the Chocolate

    ID: 20645 Type: Default 1000ms 256MiB

Sharing the Chocolate

Sharing the Chocolate

"Life is like a box of chocolates, you never know what you're gonna get."

Mingming has received a large chocolate bar that is divided into n rows and m columns – each small piece bearing a special design such as a starfish, a shell, a conch, etc. However, some pieces have been squished so badly that their designs have become unrecognizable; such pieces cannot be chosen.

Each (non‐squished) chocolate piece has a deliciousness value \(a_{i,j}\) (where \(0\le a_{i,j}\le 10^6\)); the larger the value, the more delicious the piece is.

At that moment, Zhouzhou appears and begs Mingming to share some of the chocolate. Mingming agrees under the conditions that the pieces selected must form a connected region (two pieces are connected if and only if they share a common side) and that the selection contains at least k (\(1\le k\le 5\)) different designs.

Being a bit stingy, Mingming wants to keep as much of the yummy chocolate as possible for himself. Hence, he wishes the connected selection to use as few chocolate pieces as possible. In case there are several ways to choose the fewest pieces, he would prefer the one where the median of the deliciousness values is as small as possible. (Here, for a set of n numbers, the median is defined as the \(\left\lfloor\frac{n+1}{2}\right\rfloor\)-th smallest number.)

Your task is to help Mingming determine the optimum selection.

inputFormat

The input begins with a line containing three integers n, m, and k.
The next n lines each contain m strings; each string represents the design on that chocolate piece. A piece whose design is X indicates a squished chocolate that cannot be selected.

Following this, there are n lines each containing m integers. The \(j\)-th integer on the \(i\)-th line is the deliciousness value \(a_{i,j}\) of the corresponding chocolate piece.

outputFormat

Output two integers separated by a space: the minimum number of chocolate pieces in a valid connected region and the corresponding median deliciousness value. If no valid selection exists, output -1 -1.

sample

2 2 1
star shell
X star
1 2
3 4
1 1