Chef bought an electronic board and pen. He wants to use them to record his clients' signatures.
The board is a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$) of pixels. Initially, all pixels are white. A client uses the electronic pen to sign on the board; whenever the pen touches a pixel, this pixel becomes black. Note that a signature may be non-continuous (a client may lift the pen while signing).
Chef stores a typical signature of his current client as a matrix of characters $A_{i, j}$, where for each valid $i$ and $j$, $A_{i, j}$ is either '1' (if the cell in the $i$-th row and $j$-th column is black) or '0' (if this cell is white). The client just signed on the board; this signature is stored in the same form as a matrix $B_{i, j}$. Chef wants to know how close this signature is to this client's typical signature.
Two signatures are considered the same if it is possible to choose (possibly negative) integers $dr$ and $dc$ such that for each $1 \le i \le N$ and $1 \le j \le M$, $A_{i, j} = B_{i + dr, j + dc}$. Here, if $B_{i + dr, j + dc}$ does not correspond to a valid cell, it is considered to be '0'.
To compare the signatures, the colours of zero or more cells must be flipped in such a way that the signatures become the same (each flipped cell may be in any matrix). The error in the client's current signature is the minimum number of cells whose colours must be flipped. Find the error in the signature.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $M$.
- $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a string with length $M$ describing the $i$-th row of the matrix $A$.
- $N$ more lines follow. For each valid $i$, the $i$-th of these lines contains a string with length $M$ describing the $i$-th row of the matrix $B$.

-----Output-----
For each test case, print a single line containing one integer — the error in the current signature.

-----Constraints-----
- $1 \le T \le 50$
- $2 \le N, M \le 25$

-----Example Input-----
5
3 3
100
010
000
000
010
001
4 4
0000
0110
0000
0011
1100
0000
1100
0000
3 3
100
000
001
000
010
000
3 3
000
010
000
100
000
001
3 3
111
000
000
001
001
001

-----Example Output-----
0
2
1
0
2

Source: codechef | Difficulty: HARD
Tags: Complete search


Solutions:
import numpy as np
for _ in range(int(input())):
	ans = np.float('inf')
	(n, m) = (int(x) for x in input().split())
	sig = np.zeros((n, m))
	img = np.zeros((3 * n, 3 * m))
	for row in range(n):
		sig[row, :] = np.array([int(x) for x in input()])
	for row in range(n):
		img[row + n, m:2 * m] = np.array([int(x) for x in input()])
	for i in range(2 * n):
		for j in range(2 * m):
			ans = min(ans, np.abs(np.sum(img[i:n + i, j:m + j] != sig)))
	print(ans)


from collections import defaultdict
from heapq import *
import itertools, math

def parse_integer_list():
	return [int(x) for x in input().split()]

def handle_input(N, M, A, B):
	B = ['0' * (M - 1) + B[i] + '0' * (M - 1) for i in range(N)]
	zeros = ['0' * (3 * M - 2)]
	B = zeros * (N - 1) + B + zeros * (N - 1)
	mincnt = N * M
	for i in range(2 * N - 1):
		for j in range(2 * M - 1):
			cnt = 0
			for k in range(N):
				cnt += bin(int(A[k], 2) ^ int(B[i + k][j:j + M], 2))[2:].count('1')
			mincnt = min(mincnt, cnt)
	return mincnt
	return A
T = int(input())
for i in range(T):
	(N, M) = parse_integer_list()
	A = []
	B = []
	for i in range(N):
		A.append(input())
	for i in range(N):
		B.append(input())
	print(handle_input(N, M, A, B))


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	mask = (1 << M) - 1
	arrA = [None] * N
	pixA = [None] * N
	for i in range(N):
		inp = input().strip()
		pixA[i] = inp.count('1')
		arrA[i] = int(inp, 2)
	pixels = sum(pixA)
	arrB = [None] * N
	for i in range(N):
		inp = input().strip()
		arrB[i] = int(inp, 2)
	bst = 2 * N * M
	for i in range(-N, N + 1):
		for j in range(-M, M + 1):
			res = pixels
			for k in range(N):
				if 0 <= k + i < N:
					res -= pixA[k + i]
					brow = arrB[k]
					if j < 0:
						brow <<= -j
						brow &= mask
					else:
						brow >>= j
					res += bin(brow ^ arrA[k + i]).count('1')
			bst = min(bst, res)
	print(bst)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	o = [list(map(int, input())) for r in range(n)]
	c = [list(map(int, input())) for r in range(n)]
	oe = {(i, j) for i in range(n) for j in range(m) if o[i][j]}
	(x, y) = (n + 1, m + 1)
	d = [[0] * (m + 2 * y) for i in range(x)] + [[0] * y + c[i] + [0] * y for i in range(n)] + [[0] * (m + 2 * y) for i in range(x)]
	mini = float('inf')
	for i in range(x + n):
		for j in range(y + m):
			g = [d[k][j:j + m] for k in range(i, i + n)]
			try:
				e = {(r, s) for r in range(n) for s in range(m) if g[r][s]}
			except:
				pass
			mini = min(mini, len(e.union(oe).difference(e.intersection(oe))))
	print(mini)