Under construction

Still being built. Things may change

Flood Fill

Time limit2sMemory limit1024 MB

Summary
Given two N x M binary images, find the fewest cells where A can differ from B after repeatedly flipping the color of a connected component in A.
Level

Medium7 of 10

Topics
Graph, BFS, Implementation
Solved
No attempts yet

Statement

Two black and white images AA and BB of size N×MN \times M are given.

The flood fill tool works as follows. You choose any cell (x,y)(x, y), find its connected component, and flip the colors of all cells in that component: black cells become white, and white cells become black. The connected component of a cell is the set of cells you can reach by moving up, down, left, or right without changing color.

You can apply the flood fill tool to image AA any number of times. Find the minimum possible number of cells where AA differs from BB after some sequence of operations.

Input

The first line contains two integers NN and MM (1≤N,M≤1001 \le N, M \le 100), the dimensions of the images. Each of the next NN lines contains a binary string of length MM describing the corresponding row of image AA. Each of the following NN lines contains a binary string of length MM describing the corresponding row of image BB. 0 is a white cell and 1 is a black cell.

Output

Output a single integer, the minimum possible number of cells where AA differs from BB after some sequence of operations.

Hint

In the first example, you can apply the tool to the middle cell twice. This way, the two images differ in only 1 cell.

In the second example, you can make the entire image black. This way, the two images differ in 7 cells.

Examples2

  1. Example 1

    Input
    1 3
    101
    010
    
    Expected output
    1
    
  2. Example 2

    Input
    4 4
    0001
    0101
    0101
    0111
    0000
    1110
    1110
    1110
    
    Expected output
    7