Under construction

Still being built. Things may change

Scribbling Witch

Time limit3sMemory limit512 MB

Summary
Fill the unknown cells of a grid so black cells form one tree and no two white cells share an edge, maximizing the black count.
Level

Hard8 of 10

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

Problem

The witch ALBERTINE is drawing graffiti at the entrance of a rectangular barrier made of square cells, with WW columns and HH rows. In this world, graffiti is made by painting every cell either white or black.

Her familiar ANJA has already drawn part of the graffiti. The witch wants to paint all the remaining cells and complete it. She has one condition.

  • Two different cells are called adjacent if they share an edge. In a finished graffiti, for any two black cells, there must be exactly one path from one to the other that follows adjacent black cells.
  • No two white cells may be adjacent.

The witch likes black, so she wants a finished graffiti that meets the condition and has as many black cells as possible. What is the maximum number of black cells in a graffiti that satisfies her condition?

Input

H W
c11c12…c1W
c21c22…c2W
…
cH1cH2…cHW

HH is the height of the barrier and WW is its width.

cijc_{ij} is the state of the cell in row ii from the top and column jj from the left. If the cell is painted black, cijc_{ij} is '#'. If it is painted white, cijc_{ij} is '.'. If it is not yet painted, cijc_{ij} is '?'.

Output

Print the maximum possible number of black cells in one line, when the condition is satisfied.

If there is no way to paint the graffiti that satisfies the condition, print -1.

Constraints

  • 1≤H≤111 \le H \le 11
  • 1≤W≤111 \le W \le 11
  • Each cijc_{ij} is one of '#', '.', or '?'.

Examples5

  1. Example 1

    Input
    3 4
    .##.
    #??#
    .#?#
    
    Expected output
    8
    
  2. Example 2

    Input
    5 5
    ##?##
    #???#
    ?????
    #???#
    ##?##
    
    Expected output
    19
    
  3. Example 3

    Input
    9 2
    ##
    ..
    ??
    ??
    ??
    ??
    ??
    ??
    ??
    
    Expected output
    -1
    
  4. Example 4

    Input
    7 9
    ??#####??
    ?#?????#?
    ?#?????#?
    ?#??#??#?
    ?#???#?#?
    ??#####??
    ???????#?
    
    Expected output
    44
    
  5. Example 5

    Input
    11 11
    ???????????
    ???####????
    ???#???#???
    ???#???#???
    ???#???#???
    ???####????
    ???#???#???
    ???#???#???
    ???#???#???
    ???####????
    ???????????
    
    Expected output
    86