import sys
N, M = map(int, sys.stdin.readline().split())
matrixBefore = []
matrixAfter = []
for _ in range(N):
matrixBefore.append(list(map(int, sys.stdin.readline().rstrip())))
for _ in range(N):
matrixAfter.append(list(map(int, sys.stdin.readline().rstrip())))
def reverse(matrix, row, column):
for r in range(row, row + 3):
for c in range(column, column + 3):
matrix[r][c] = ~matrix[r][c] + 2
return matrix
def solution(matrixBefore, matrixAfter):
count = 0
for row in range(N - 2):
for column in range(M - 2):
if matrixBefore[row][column] != matrixAfter[row][column]:
count += 1
matrixBefore = reverse(matrixBefore, row, column)
if matrixBefore[row] != matrixAfter[row]:
return -1
if matrixBefore != matrixAfter:
return -1
return count
print(solution(matrixBefore, matrixAfter))
원래는 N이나 M이 3이하일 때 -1을 출력하는 코드를 작성했다. 하지만 애초에 두 행렬이 같을 경우 정답이 0이 된다.
1 1
1
1