This page is still under construction.

Parts of this page are still being built. What you see may change.

Making a Digram

Time limit1sMemory limit256 MB

Summary
Place one of the seven-square digram shapes on an n by m grid to minimize the cost of adding and erasing cells so the black cells match the shape.
Level

Medium6 of 10

Topics
Brute force, Implementation, Prefix sum, Simulation
Solved
No attempts yet

Problem

In 2021, making a digram on the spot became a basic skill for Koreans. We want to draw a digram on the n×mn \times m grid in front of us.

A digram is defined as a shape made by joining seven k×kk \times k squares. The following are digrams for k=1k=1 and k=2k=2.

Examples of shapes that are not digrams are as follows.

Not made of seven k×kk \times k squaresA digram cannot be flipped or rotated

Some cells of the grid are already colored black. Painting a white cell black costs aa, and erasing a black cell to make it white costs bb. Write a program that finds the minimum cost needed so that the black cells form a digram.

The position and size of the digram are not restricted, but it cannot be flipped or rotated, and it must not go outside the grid. Also, every black cell must be included in the digram, and every cell not included in the digram must be white.

Input

The first line gives the size of the grid, n,mn, m.

The second line gives the costs a,ba, b of changing a cell's color.

The next nn lines each give a string of length mm. # is a cell colored black, and . is a white cell.

Output

On the first line, print the minimum cost to make a digram.

Constraints

  • 3≤n,m≤203 \le n,m \le 20
  • 1≤a,b≤10001 \le a,b \le 1000

Examples4

  1. Example 1

    Input
    3 3
    2 5
    #.#
    .#.
    #.#
    
    Expected output
    11
    
  2. Example 2

    Input
    6 7
    10 15
    .#####.
    .#####.
    .#.....
    .#.....
    .#####.
    .#####.
    
    Expected output
    60
    
  3. Example 3

    Input
    8 8
    1000 1
    ..#..#..
    .#..#..#
    #..#..#.
    ..#..#..
    .#..#..#
    #..#..#.
    ..#..#..
    .#..#..#
    
    Expected output
    4018
    
  4. Example 4

    Input
    8 8
    1 1000
    ..#..#..
    .#..#..#
    #..#..#.
    ..#..#..
    .#..#..#
    #..#..#.
    ..#..#..
    .#..#..#
    
    Expected output
    11018