문제출처 : https://www.acmicpc.net/problem/1080
a,b의 값이 다를 경우 True인 check matrix를 만들어 준다.
y-3,x-3까지의 범위를 살피며 True일 경우 값을 바꿔준다.
(그 이외의 범위는 index error가 날 수 있으며 누를 수 없기 때문이다.)
과정이 끝난후 check matrix에 True 값이 남아있다면 -1을 return
아니면 cnt 를 return 한다.
import sys
def solve():
input = sys.stdin.readline
y,x = map(int,input().split())
a = [input()[:-1] for i in range(y)]
b = [input()[:-1] for i in range(y)]
if y<3 or x<3:
if a != b:
return print(-1)
else:
return print(0)
check = [[] for i in range(y)]
cnt = 0
for i in range(y):
for j in range(x):
if a[i][j] == b[i][j]:
check[i].append(False)
else:
check[i].append(True)
for i in range(y-2):
for j in range(x-2):
if check[i][j] == True:
cnt+= 1
for _y in range(3):
for _x in range(3):
if check[i+_y][j+_x] == True:
check[i+_y][j+_x] = False
else:
check[i+_y][j+_x] = True
for i in range(y):
for j in range(x):
if check[i][j] == True:
return print(-1)
return print(cnt)
solve()