r, c = map(int, input().split())
trees = [[int(x) for x in input().split()] for _ in range(r)]
s, t = map(int, input().split())
building = [list(input()) for _ in range(s)]
ans = 100 * r * c
for _ in range(4):
    for i in range(r - s + 1):
        for j in range(c - t + 1):
            cur = sum(trees[i + x][j + y] for x in range(s) for y in range(t) if building[x][y] == '#')
            ans = min(ans, cur)
    building = zip(*building)
    building = [row[::-1] for row in building]
    s, t = t, s

print(sum(sum(row) for row in trees) - ans)
