atcoder#ABC211E. [ABC211E] Red Polyomino

[ABC211E] Red Polyomino

Score : 500500 points

Problem Statement

You are given a grid with NN rows and NN columns, where the square at the ii-th row from the top and jj-th column from the left is painted black if Si,jS_{i, j} is # and white if Si,jS_{i, j} is .. You will choose KK of the white squares and paint them red. How many such ways to paint the grid satisfy the following condition?

  • The squares painted red will be connected. That is, you will be able to get from any red square to any red square by repeatedly moving horizontally or vertically while only visiting red squares.

Constraints

  • 1N81 \leq N \leq 8
  • 1K81 \leq K \leq 8
  • Each Si,jS_{i, j} is # or ..
  • NN and KK are integers.

Input

Input is given from Standard Input in the following format:

NN

KK

S1,1S1,2S1,NS_{1, 1}S_{1, 2} \dots S_{1, N}

S2,1S2,2S2,NS_{2, 1}S_{2, 2} \dots S_{2, N}

\vdots

SN,1SN,2SN,NS_{N, 1}S_{N, 2} \dots S_{N, N}

Output

Print the answer.

3
5
#.#
...
..#
5

We have five ways to satisfy the condition as shown below, where @ stands for a red square.

#.# #@# #@# #@# #@#
@@@ .@@ @@. @@@ @@@
@@# @@# @@# .@# @.#

Note that the way shown below does not satisfy the connectivity requirement since we do not consider diagonal adjacency.

#@#
@.@
@@#
2
2
#.
.#
0

There is no way to satisfy the condition.

8
8
........
........
........
........
........
........
........
........
64678