Flood Fill

시간 제한2초메모리 제한1024 MB

요약
같은 색 연결 성분을 뒤집는 플러드 필을 여러 번 적용해 A와 B가 다른 칸 수의 최솟값을 구한다.
난이도

어려움10점 중 8점

유형
그래프, BFS, 그리디
정답자
아직 제출이 없습니다

문제

Given are two black and white N×MN \times M images AA and BB.

The "flood fill" tool works as follows: you choose any cell (x,y)(x, y), locate its connected component and flip the colors of all the cells in the component (if the cell was black, it becomes white, and if it was white, it becomes black). The connected component of the cell is the set of cells you can reach by going up/down/left/right without changing color.

You can apply the "flood fill" tool to image AA any number of times. What is the minimum number of cells in which AA can be different from BB after some sequence of operations?

입력

The first line of input 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 the image AA.

Each of the next NN lines contains a binary string of length MM, describing the corresponding row of the image BB.

Here 0 corresponds to the cell colored white, 1 corresponds to the cell colored black.

출력

Output a single integer --- the minimum possible number of cells in which AA can be different from BB after some sequence of operations.

힌트

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

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

예제2

  1. 예제 1

    입력
    1 3
    101
    010
    
    예상 출력
    1
    
  2. 예제 2

    입력
    4 4
    0001
    0101
    0101
    0111
    0000
    1110
    1110
    1110
    
    예상 출력
    7