This page is still under construction.

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

Watering Plan Check 3

Time limit1sMemory limit128 MB

Summary
Given a grid land divided into fields and a proposed sprinkler plan drawn with letters and underscores, verify the plan and count holes drilled in fences.
Level

Medium6 of 10

Topics
Implementation, Simulation, Graph, DFS
Solved
No attempts yet

Problem

Sara farms a large rectangular piece of land. The land is a grid of 5R5R rows and 5C5C columns. A horizontal fence runs across it after every fifth row and a vertical fence runs down it after every fifth column, so the fences cut the land into R×CR \times C areas of 5×55 \times 5 cells called fields.

To keep birds off the crops, some fields have a scarecrow. A scarecrow takes up a single cell, and a field holds at most one.

During a drought Sara waters the crops with sprinklers. A sprinkler has three nozzles: one main nozzle and two side nozzles. Each side nozzle sits in a cell next to the main nozzle (up, down, left or right), so one sprinkler takes up exactly three cells and waters all three. The three cells form a straight line or an L.

A watering plan places sprinklers so that exactly one sprinkler waters every cell without a scarecrow. A cell with a scarecrow holds no nozzle, and no nozzle sits outside the land.

The three cells of one sprinkler need not lie in the same field. When they reach into a neighbouring field, Sara drills a hole in the fence between the two cells.

A plan is written as a picture in the same shape as the land. Every empty cell becomes a lowercase letter, and every fence character between two cells watered by the same sprinkler becomes an underscore _. The letters follow these rules.

  1. The three cells watered by one sprinkler carry the same letter, even when they lie in different fields.
  2. Two adjacent cells in the same field watered by different sprinklers carry different letters.
  3. Two adjacent cells in neighbouring fields watered by different sprinklers with a hole between them carry different letters.
  4. Two adjacent cells in different fields may carry the same letter as long as the rules above hold.

These rules make the sprinklers readable from the plan alone. Two adjacent cells belong to the same sprinkler when they carry the same letter and either lie in the same field or have a hole between them. One group of cells linked this way is one sprinkler.

You are given the land and a plan. Decide whether the plan is valid and, if it is, count the holes drilled in the fences. A plan is valid when all three conditions below hold.

  1. Every scarecrow cell holds # and every empty cell holds one lowercase letter. Every fence position holds its original fence character or an underscore, and every + where fences meet stays +.
  2. Every group read off the plan covers exactly three cells.
  3. The two cells on either side of an underscore carry the same lowercase letter.

Input

The first line contains RR and CC (1≤R,C≤1001 \le R, C \le 100).

The next 6R−16R-1 lines contain 6C−16C-1 characters each and describe the land. One cell is one character. A dot . is an empty cell and # (ASCII 35) is a scarecrow. A vertical fence is | (ASCII 124), a horizontal fence is - (minus), and + marks a place where fences meet. The fence has no thickness, but it is drawn with characters.

The next 6R−16R-1 lines give the plan in the same shape. Each of those lines consists of English letters and the characters #, |, -, + and _. The plan is not guaranteed to be valid.

Output

Print the number of holes drilled in the fences if the plan is valid. Print -1 otherwise.

Examples4

  1. Example 1

    Input
    2 2
    .....|.....
    .....|.....
    ...#.|.....
    .....|.....
    .....|.....
    -----+-----
    .....|.....
    .....|.....
    .....|.....
    .....|.....
    .....|.....
    aaacc|dxxxa
    bbbce|dyyya
    ddd#e|dzzza
    ccbae|fccbb
    cbbaa|ffcdb
    -----+---_-
    ssrrr|tttdd
    saaax_xxeee
    yxbbb|zdaaa
    yxccc|zdbbb
    yxddd|zdccc
    
    Expected output
    2
    
  2. Example 2

    Input
    1 1
    .....
    .....
    ..#..
    .....
    .....
    aaabb
    cccba
    aa#aa
    abbbc
    dddcc
    
    Expected output
    0
    
  3. Example 3

    Input
    1 3
    .....|.....|.....
    .....|.....|.....
    .....|.....|.....
    .....|.....|.....
    .....|.....|.....
    aaabb_baaab_bbaaa
    bbbaa_abbba_aabbb
    aaabb_baaab_bbaaa
    bbbaa_abbba_aabbb
    aaabb_baaab_bbaaa
    
    Expected output
    10
    
  4. Example 4

    Input
    1 3
    .....|.....|.....
    .....|.....|.....
    .....|.....|.....
    .....|.....|.....
    .....|.....|.....
    aaabb|baaab_bbaaa
    bbbaa_abbba_aabbb
    aaabb_baaab_bbaaa
    bbbaa_abbba_aabbb
    aaabb_baaab_bbaaa
    
    Expected output
    -1