Time limit
2s
Memory limit
128 MB
You are given two matrices A and B consisting only of 0s and 1s. In one operation, choose any 3×3 submatrix and flip every element inside it: 0 becomes 1, and 1 becomes 0.
Find the minimum number of operations needed to make matrix A equal to matrix B.
The first line contains the matrix dimensions N and M. Both N and M are natural numbers not greater than 50.
The next N lines contain matrix A, followed by N lines containing matrix B. Each row is a length-M string consisting only of 0s and 1s.
Print the minimum number of operations needed to transform matrix A into matrix B. If it is impossible, print -1.