cswork / OAOpen practice workspace

OpenAIOA #1

Matching Regions in Two Grids

Estimated MediumJudge-ready

Topics (AI-labelled)Grid Search

In each of the two 01 grids, partition the 1 cells into connected components using up/down/left/right adjacency. Count how many components in the second grid have a full coordinate set exactly equal to some full component in the first grid. This is neither a sub-island check nor a same-shape-after-translation check.

Input

The first line contains n m, followed by n lines of the first grid and then n lines of the second grid. Each line has m consecutive 0/1 characters. This site uses 1≤n,m≤200.

Output

A single integer, the number of exactly matching components.

Example 1

Input:

3 3
111
100
100
111
100
101

Output:

1

Example 2

Input:

1 2
11
10

Output:

0

Example 3

Input:

2 2
10
01
10
01

Output:

2

Example notes

In Example 1 only the large component matches, so the answer is 1. In Example 2 the second grid is only a proper subset of the first, so the answer is 0. In Example 3 diagonal cells are not connected, so there are two matching components.

Editorial

Loading solution…