Office Building

시간 제한1초메모리 제한2048 MB

요약
회전만 가능한 다연결 도형을 격자에 배치해 잘리는 나무 나이 합의 최솟값을 구하고, 전체 나이 합에서 그 값을 뺀 결과를 출력한다.
난이도

보통10점 중 5점

유형
시뮬레이션, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

Company Z has purchased a land lot for their new office building. The land is shaped like a rectangular grid with rr rows and cc columns, in which each cell has a tree. The age of each tree is known.

Because Company Z is at the innovative front of the world, their new office building will not just be rectangular. Instead, it will have some exotic shape and a very special floor plan. The shape can be represented by some connected grid cells. There is no particular facing requirement of the building, and the floor plan can be rotated by 9090 degrees an arbitrary number of times. However, the floor plan cannot be flipped vertically or horizontally.

Company Z wants to choose a building location within their land lot. The trees in those cells that are occupied by the building will have to be cut down. Company Z would like to preserve the trees so that the sum of age of the remaining trees is as large as possible. Could you help them choose the best location for their new office building?

입력

The first line of input contains two integers rr and cc (1≤r,c≤201 ≤ r, c ≤ 20), the dimensions of the land lot.

The next rr lines each contain cc integers between 11 and 100100 (both inclusive) describing the ages of the trees in the land lot.

The next line contains two integer ss and tt (1≤s≤r1 ≤ s ≤ r, 1≤t≤c1 ≤ t ≤ c), the dimensions of the floor plan.

The next ss lines each contain tt characters that describe the floor plan of the building. A hash (#) denotes a cell occupied by the building and a dot (.) denotes a non-occupied cell. There is at least one hash. It is guaranteed that the hashes form a connected shape. Two hashes are directly connected if their cells share a side, and a shape is connected when all its hashes are either directly or indirectly connected. There is no row or column in the floor plan that is completely empty.

출력

Output a single integer, the maximum sum of age of the trees that can be preserved.

예제2

  1. 예제 1

    입력
    4 5
    4 3 1 1 1
    5 4 3 5 1
    9 6 2 1 1
    8 7 1 1 2
    3 4
    ###.
    #.##
    #..#
    
    예상 출력
    58
    
  2. 예제 2

    입력
    3 3
    6 6 6
    3 6 1
    1 1 1
    2 3
    #..
    ###
    
    예상 출력
    25