Flood Fill
Time limit2sMemory limit1024 MB
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 and of size are given.
The flood fill tool works as follows. You choose any cell , 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 any number of times. Find the minimum possible number of cells where differs from after some sequence of operations.
Input
The first line contains two integers and (), the dimensions of the images. Each of the next lines contains a binary string of length describing the corresponding row of image . Each of the following lines contains a binary string of length describing the corresponding row of image . 0 is a white cell and 1 is a black cell.
Output
Output a single integer, the minimum possible number of cells where differs from 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.
