You are given a square grid with $n$ rows and $n$ columns. Each cell contains either $0$ or $1$.

In an operation, you can select a cell of the grid and flip it (from $0 \to 1$ or $1 \to 0$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $0^{\circ}$, $90^{\circ}$, $180^{\circ}$ and $270^{\circ}$.

The picture below shows an example of all rotations of a grid.


-----Input-----

The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 100$) — the size of the grid.

Then $n$ lines follow, each with $n$ characters $a_{i,j}$ ($0 \leq a_{i,j} \leq 1$) — the number written in each cell.


-----Output-----

For each test case output a single integer  — the minimum number of operations needed to make the square look the same rotated $0^{\circ}$, $90^{\circ}$, $180^{\circ}$ and $270^{\circ}$.


-----Examples-----

Input
5
3
010
110
010
1
0
5
11100
11011
01011
10011
11000
5
01000
10101
01010
00010
01001
5
11001
00000
11111
10110
01111
Output
1
0
9
7
6


-----Note-----

In the first test case, we can perform one operations to make the grid $\begin{matrix}0 & 1 & 0\\ 1 & 1 & {1}\\ 0 & 1 & 0\end{matrix}$. Now, all rotations of the square are the same.

In the second test case, all rotations of the square are already the same, so we don't need any flips.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
def solveodd(a, n):
	ans = 0
	for j in range(int((n + 1) / 2)):
		for k in range(int((n - 1) / 2)):
			b = 0
			if a[j][k] == '1':
				b += 1
			if a[n - 1 - k][j] == '1':
				b += 1
			if a[n - 1 - j][n - 1 - k] == '1':
				b += 1
			if a[k][n - 1 - j] == '1':
				b += 1
			if b == 1 or b == 3:
				ans += 1
			elif b == 2:
				ans += 2
	return ans

def solveeven(a, n):
	ans = 0
	for j in range(int(n / 2)):
		for k in range(int(n / 2)):
			b = 0
			if a[j][k] == '1':
				b += 1
			if a[n - 1 - k][j] == '1':
				b += 1
			if a[n - 1 - j][n - 1 - k] == '1':
				b += 1
			if a[k][n - 1 - j] == '1':
				b += 1
			if b == 1 or b == 3:
				ans += 1
			elif b == 2:
				ans += 2
	return ans
t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		a.append(input())
	if n % 2 == 1:
		print(solveodd(a, n))
	else:
		print(solveeven(a, n))


def check(a1, a2, a3, a4):
	k = 0
	for i in range(len(a1)):
		S = [a1[i], a2[i], a3[i], a4[i]]
		k += min(S.count('0'), S.count('1'))
	return k
t = int(input())
for _ in range(t):
	n = int(input())
	M = []
	for _ in range(n):
		M.append(list(input()))
	m = 0
	(x, y) = (0, n)
	while x < y - 1:
		m += check(M[x][x:y - 1], M[y - 1][x + 1:y][::-1], [M[i][y - 1] for i in range(x, y - 1)], [M[i][x] for i in range(x + 1, y)][::-1])
		x += 1
		y -= 1
	print(m)


def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		arr = []
		for _ in range(N):
			arr.append(list(input()))
		ans = 0
		for i in range(N // 2 + 1):
			k = 0
			for j in range(i, N - i - 1):
				countZeroes = 0
				if arr[i][j] == '0':
					countZeroes += 1
				if arr[i + k][N - i - 1] == '0':
					countZeroes += 1
				if arr[N - i - 1][N - j - 1] == '0':
					countZeroes += 1
				if arr[N - j - 1][i] == '0':
					countZeroes += 1
				countOnes = 4 - countZeroes
				if countZeroes <= countOnes:
					ans += countZeroes
				else:
					ans += countOnes
				k += 1
		print(ans)
main()


def solve():
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(input()))
	if n == 1:
		print(0)
	else:
		a = 0
		b = n - 1
		ans = 0
		for i in range(n // 2):
			for j in range(a, b):
				one = 0
				zero = 0
				if l[a][j] == '0':
					zero += 1
				else:
					one += 1
				w = n - a - 1
				if l[j][w] == '0':
					zero += 1
				else:
					one += 1
				if l[w][n - j - 1] == '0':
					zero += 1
				else:
					one += 1
				if l[n - j - 1][n - w - 1] == '0':
					zero += 1
				else:
					one += 1
				if one < zero:
					ans += one
				else:
					ans += zero
			a += 1
			b -= 1
		print(ans)
t = int(input())
while t:
	solve()
	t -= 1


def solve_task():
	grid_size = int(input())
	grid = [input() for _ in range(grid_size)]
	change_cnt = 0
	for offset in range(grid_size // 2):
		(top_left_row, top_left_col) = (offset, offset)
		(top_right_row, top_right_col) = (offset, grid_size - offset - 1)
		(bottom_right_row, bottom_right_col) = (grid_size - offset - 1, grid_size - offset - 1)
		(bottom_left_row, bottom_left_col) = (grid_size - offset - 1, offset)
		for _ in range(grid_size - offset * 2 - 1):
			top_left_col += 1
			top_right_row += 1
			bottom_right_col -= 1
			bottom_left_row -= 1
			selected_cells = [grid[top_left_row][top_left_col], grid[top_right_row][top_right_col], grid[bottom_right_row][bottom_right_col], grid[bottom_left_row][bottom_left_col]]
			zeroes = selected_cells.count('0')
			ones = selected_cells.count('1')
			change_cnt += 4 - max(zeroes, ones)
	return change_cnt
testcases_cnt = int(input())
while testcases_cnt:
	print(solve_task())
	testcases_cnt -= 1


def simresh():
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	ans = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			k = l[i][j] + l[n - 1 - i][n - 1 - j] + l[j][n - 1 - i] + l[n - 1 - j][i]
			u = k.count('1')
			if u > 2:
				u = 4 - u
			ans += u
	print(ans)
	return
n = int(input())
for i in range(n):
	simresh()


ans = []
test_cases = int(input(''))
for i in range(test_cases):
	dim = int(input(''))
	mat = []
	for j in range(dim):
		mat.append(list(input('')))
	if dim == 1:
		ans.append(0)
		continue
	elif dim == 2:
		count = (mat[0].count('1') + mat[1].count('1')) % 4
		if count == 3:
			ans.append(1)
		else:
			ans.append(count)
		continue
	elif dim % 2 == 0:
		(a, b) = (dim // 2, dim // 2)
	else:
		(a, b) = (dim // 2 + 1, dim // 2)
	count = 0
	for r in range(a):
		for c in range(b):
			temp_count = [mat[r][c], mat[dim - 1 - c][r], mat[dim - 1 - r][dim - 1 - c], mat[c][dim - 1 - r]].count('1') % 4
			if temp_count == 3:
				count += 1
			else:
				count += temp_count
	ans.append(count)
for b in ans:
	print(b)


from math import ceil
test = int(input())
while test:
	n = int(input())
	k = n
	a = [input() for _ in range(n)]
	ans = 0
	n -= 1
	row = 0
	for j in range(ceil(n / 2)):
		for i in range(row, k - 1):
			ele = [a[row][i], a[n - row][n - i], a[i][n - row], a[n - i][row]]
			zero = ele.count('0')
			one = 4 - zero
			if zero == 4 or one == 4:
				ans += 0
			elif zero == 3 or one == 3:
				ans += 1
			elif zero == 2 or one == 2:
				ans += 2
		row += 1
		k -= 1
	print(ans)
	test -= 1


def solve():
	ans = 0
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			ones = 0
			zeros = 0
			if grid[i][j] == '0':
				zeros += 1
			else:
				ones += 1
			if grid[j][n - 1 - i] == '0':
				zeros += 1
			else:
				ones += 1
			if grid[n - 1 - i][n - 1 - j] == '0':
				zeros += 1
			else:
				ones += 1
			if grid[n - 1 - j][i] == '0':
				zeros += 1
			else:
				ones += 1
			ans += min(ones, zeros)
	return ans
T = int(input())
for t in range(T):
	n = int(input())
	grid = []
	for i in range(n):
		grid.append(input())
	ans = solve()
	print(ans)


def main():
	for t in range(int(input())):
		n = int(input())
		g = [''] + ['0' + input() for i in range(n)]
		ans = 0
		for i in range(1, n // 2 + 1):
			for j in range(1, n // 2 + 1):
				(x, y) = (0, 0)
				if g[i][j] == '0':
					x += 1
				else:
					y += 1
				if g[j][n - i + 1] == '0':
					x += 1
				else:
					y += 1
				if g[n - i + 1][n - j + 1] == '0':
					x += 1
				else:
					y += 1
				if g[n - j + 1][i] == '0':
					x += 1
				else:
					y += 1
				ans += min(x, y)
		if n % 2 == 1:
			for i in range(1, n // 2 + 1):
				(x, y) = (0, 0)
				if g[i][n // 2 + 1] == '0':
					x += 1
				else:
					y += 1
				if g[n // 2 + 1][n - i + 1] == '0':
					x += 1
				else:
					y += 1
				if g[n - i + 1][n // 2 + 1] == '0':
					x += 1
				else:
					y += 1
				if g[n // 2 + 1][i] == '0':
					x += 1
				else:
					y += 1
				ans += min(x, y)
		print(ans)
main()


from typing import List
import math

def get_input() -> List[List]:
	n = int(input())
	ret = []
	for i in range(n):
		c = int(input())
		case = []
		for j in range(c):
			li = str(input())
			case.append(li)
		ret.append(case)
	return ret

def get_mirror_elems(case):
	n = len(case)
	sub_n = math.ceil(n / 2)
	sub_i = sub_n
	if n % 2 != 0:
		sub_i = sub_n - 1
	elem_list = []
	for i in range(sub_i):
		for j in range(sub_n):
			elem_list.append([case[i][j], case[n - i - 1][n - j - 1], case[n - j - 1][i], case[j][n - i - 1]])
	return elem_list

def get_flip_number_of_list(li):
	n0 = li.count('0')
	n1 = len(li) - n0
	return min(n1, n0)

def solve_one_case(case):
	elem_lists = get_mirror_elems(case)
	slip_count = 0
	for li in elem_lists:
		slip_count += get_flip_number_of_list(li)
	return slip_count

def print_int(ans) -> None:
	print(ans)

def print_list(ans: list) -> None:
	for x in ans:
		print(x)
cases = get_input()
for case in cases:
	result = solve_one_case(case)
	print(result)


def calc(top, bottom, left, right):
	tmp_out = 0
	for x in range(len(top) - 1):
		tmp_arr = [top[x], right[x], bottom[x], left[x]]
		tmp_out += min(tmp_arr.count('0'), tmp_arr.count('1'))
	return tmp_out
for _ in range(int(input())):
	n = int(input())
	arr = []
	out = 0
	for __ in range(n):
		arr.append(input())
	for x in range(n // 2):
		current_top = arr[x][x:n - x]
		current_bottom = arr[n - x - 1][x:n - x]
		current_left = []
		current_right = []
		for y in range(x, n - x):
			current_left.append(arr[y][x])
			current_right.append(arr[y][n - x - 1])
		current_left = ''.join(current_left)
		current_right = ''.join(current_right)
		out += calc(current_top, current_bottom[::-1], current_left[::-1], current_right)
	print(out)


I = input
for _ in [0] * int(I()):
	n = int(I())
	a = [I() for _ in [0] * n]
	print(sum((min(map([a[k][l] for (k, l) in ((i, j), (j, ~i), (~i, ~j), (~j, i))].count, '01')) for i in range(n + 1 >> 1) for j in range(n >> 1))))


x = int(input())

def cal(n, arr):
	if n % 2 == 0:
		a = b = n // 2
	else:
		(a, b) = (n // 2, n // 2 + 1)
	res = 0
	for i in range(a):
		for j in range(b):
			t = arr[i][j] + arr[j][n - 1 - i] + arr[n - 1 - i][n - 1 - j] + arr[n - 1 - j][i]
			if t == 1 or t == 3:
				res += 1
			elif t == 2:
				res += 2
	return res
for jj in range(x):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append([int(_) for _ in list(input())])
	print(cal(n, arr))


from sys import stdin
import math

def read():
	return stdin.readline().strip()
num = [0, 1, 2, 1, 0]
for _ in range(int(read())):
	n = int(read())
	matrix = [list(map(int, read())) for _ in range(n)]
	N = n - 1
	s = 0
	for i in range(math.ceil(n / 2.0)):
		for j in range(math.floor(n / 2.0)):
			sum = matrix[i][j] + matrix[N - j][i] + matrix[N - i][N - j] + matrix[j][N - i]
			s += num[sum]
	print(s)


import math

def solve():
	n = int(input())
	l = []
	ans = 0
	for _ in range(n):
		l.append(list(map(int, input())))
	m = math.ceil(n / 2)
	if n % 2 == 0:
		(p, q) = (m, m)
	else:
		(p, q) = (m - 1, m)
	for i in range(p):
		for j in range(q):
			s = l[i][j] + l[j][n - i - 1] + l[n - j - 1][i] + l[n - i - 1][n - j - 1]
			if s == 0 or s == 1 or s == 2:
				ans = ans + s
			else:
				ans = ans + 4 - s
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	s = list()
	count = 0
	for i in range(n):
		s.append(input())
	y = 0
	while n // 2 + n % 2 - y - 1 >= 0:
		for x in range(0 + y, n - y - 1):
			unmatch = 0
			if s[y][x] != s[x][n - y - 1]:
				unmatch += 1
			if s[y][x] != s[n - y - 1][n - x - 1]:
				unmatch += 1
			if s[y][x] != s[n - x - 1][y]:
				unmatch += 1
			if unmatch in (1, 3):
				count += 1
			elif unmatch in (2,):
				count += 2
		y += 1
	print(count)


def fn(arr):
	n = len(arr)
	c = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			temp = arr[i][j] + arr[j][n - i - 1] + arr[n - i - 1][n - j - 1] + arr[n - j - 1][i]
			c += min(temp, 4 - temp)
	return c
t = int(input())
for T in range(t):
	n = int(input())
	arr = [list(map(int, input())) for i in range(n)]
	st = fn(arr)
	print(st)


for _ in range(int(input())):
	n = int(input())
	a = [input() for _ in range(n)]
	counter = 0
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			x = (a[i][j] + a[j][n - i - 1] + a[n - i - 1][n - j - 1] + a[n - j - 1][i]).count('1')
			counter += 2 - abs(x - 2)
	print(counter)


t = int(input())
for test in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(list(input()))
	ans = 0
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			count = 0
			if a[i][j] == '1':
				count += 1
			if a[j][-(i + 1)] == '1':
				count += 1
			if a[-(i + 1)][n - 1 - j] == '1':
				count += 1
			if a[n - 1 - j][i] == '1':
				count += 1
			if count == 1 or count == 3:
				ans += 1
			elif count == 2:
				ans += 2
	print(ans)


X = [0, 1, 2, 1, 0]
for t in range(int(input())):
	n = int(input())
	S = 0
	A = []
	for j in range(n):
		A.append(list(map(int, list(input()))))
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			s = A[i][j] + A[j][n - i - 1] + A[n - j - 1][i] + A[n - i - 1][n - j - 1]
			S += X[s]
	print(S)


s = ''
for _ in range(int(input())):
	n = int(input())
	sp = [input() for _ in range(n)]
	k = 0
	for y in range(n // 2 + n % 2):
		for x in range(n // 2):
			a = sp[x][y] + sp[n - 1 - y][x] + sp[y][n - 1 - x] + sp[n - 1 - x][n - 1 - y]
			k += min(a.count('0'), a.count('1'))
	s += str(k) + '\n'
print(s)


import math
for _ in range(int(input())):
	n = int(input())
	a = []
	for __ in range(n):
		a.append(input())
	m = 0
	for i in range(math.ceil(n / 2)):
		for j in range(i, n - i - 1):
			c = 0
			if a[i][j] != a[j][n - i - 1]:
				c += 1
			if a[i][j] != a[n - i - 1][n - j - 1]:
				c += 1
			if a[i][j] != a[n - j - 1][i]:
				c += 1
			if c == 3:
				m += 1
			else:
				m += c
	print(m)


case = int(input().strip())
for i in range(case):
	n = int(input().strip())
	l = []
	a = 0
	for j in range(n):
		line = input().strip()
		l.append(list(line))
	n2 = len(l[0]) - 1
	for i in range(len(l) - 1):
		for j in range(i, n2):
			new = [l[i][j], l[n - i - 1][n - j - 1], l[n - j - 1][i], l[j][n - i - 1]]
			flip = min(new.count('0'), new.count('1'))
			a += flip
		n2 = n2 - 1
	print(a)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	matrix = []
	for i in range(0, n):
		temp = input()
		matrix.append(temp)
	i = 0
	m = n - 1
	flip = 0
	for k in range(n - 1, 0, -2):
		for j in range(0, k):
			count1 = 0
			if matrix[i][j + i] == '1':
				count1 += 1
			if matrix[j + i][m] == '1':
				count1 += 1
			if matrix[m][m - j] == '1':
				count1 += 1
			if matrix[m - j][i] == '1':
				count1 += 1
			flip += min(count1, abs(4 - count1))
		i += 1
		m -= 1
	print(flip)


def solve():
	n = int(input())
	arr = [[0] for _ in range(n)]
	for i in range(n):
		row = list(map(int, list(input())))
		arr[i] = row
	res = 0
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			count = arr[i][j]
			count += arr[j][n - i - 1]
			count += arr[n - i - 1][n - j - 1]
			count += arr[n - j - 1][i]
			res += min(count, 4 - count)
	print(res)
n_testcase = int(input())
for _ in range(n_testcase):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = [input() for _ in range(n)]
	kq = 0
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			cur = (a[i][j] + a[n - j - 1][i] + a[j][n - i - 1] + a[n - i - 1][n - j - 1]).count('1')
			cur = min(cur, 4 - cur)
			kq += cur
	print(kq)


def solve(mtx):
	m = len(mtx) - 1
	i = 0
	min_ops = 0
	while i < len(mtx) // 2:
		for k in range(i, len(mtx) - 1 - i):
			tmp = [int(mtx[i][k]), int(mtx[k][m - i]), int(mtx[m - i][m - k]), int(mtx[m - k][i])]
			min_ops += min(4 - sum(tmp), sum(tmp))
		i += 1
	return min_ops
t = int(input())
res = []
for _ in range(t):
	n = int(input())
	matrix = []
	for _ in range(n):
		matrix.append(input())
	res.append(solve(matrix))
for i in res:
	print(i)


for _ in range(int(input())):
	n = int(input())
	a = [list(map(int, input())) for _ in range(n)]
	total = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			total += 2 - abs(a[i][j] + a[j][n - 1 - i] + a[n - 1 - i][n - 1 - j] + a[n - 1 - j][i] - 2)
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	L = []
	for i in range(n):
		L.append(input())
	(l, r) = (0, n - 1)
	cnt = 0
	while l < r:
		for i in range(l, r):
			(ones, zeros) = (0, 0)
			if L[l][i] == '0':
				zeros += 1
			else:
				ones += 1
			if L[i][n - 1 - l] == '0':
				zeros += 1
			else:
				ones += 1
			if L[n - 1 - l][n - i - 1] == '0':
				zeros += 1
			else:
				ones += 1
			if L[n - 1 - i][l] == '0':
				zeros += 1
			else:
				ones += 1
			cnt += min(zeros, ones)
		l += 1
		r -= 1
	print(cnt)


q = int(input())
for p in range(q):
	n = int(input())
	m = []
	res = 0
	for i in range(n):
		m.append(input())
	dop_n = n
	n -= 1
	for i in range(dop_n // 2):
		for j in range(i, n - i):
			s = int(m[i][j]) + int(m[n - i][n - j]) + int(m[j][n - i]) + int(m[n - j][i])
			if s % 4 == 0:
				continue
			res += min(s, 4 - s)
	print(res)


def solve(m, x, y):
	n = len(m)
	sm = m[y][x] + m[n - 1 - x][y] + m[n - 1 - y][n - 1 - x] + m[x][n - 1 - y]
	return sm if sm <= 2 else 4 - sm
for t in range(int(input())):
	n = int(input())
	m = [list(map(int, list(input()))) for _ in range(n)]
	res = 0
	for y in range(n // 2):
		for x in range(y, n - y - 1, 1):
			res = res + solve(m, x, y)
	print(res)


for _ in range(int(input())):
	n = int(input())
	mat = []
	for i in range(n):
		mat.append(list(map(int, [j for j in input()])))
	ans = 0
	for i in range(n // 2):
		for j in range(i, n - 1 - i):
			s = mat[i][j] + mat[j][n - 1 - i] + mat[n - 1 - i][n - 1 - j] + mat[n - 1 - j][i]
			if s == 1 or s == 3:
				ans += 1
			elif s == 2:
				ans += 2
	print(ans)


out = []
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	ans = 0
	for i in range((n - 1) // 2 + 2):
		for j in range(i, n - i - 1):
			c = []
			c.append(a[i][j])
			c.append(a[j][n - i - 1])
			c.append(a[n - i - 1][n - j - 1])
			c.append(a[n - j - 1][i])
			ans += min(c.count('0'), c.count('1'))
	out.append(ans)
for i in out:
	print(i)


t = int(input())
for q in range(t):
	n = int(input())
	a = [list(input()) for i in range(n)]
	count = 0
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			now = a[i][j] + a[j][n - i - 1] + a[n - i - 1][n - j - 1] + a[n - j - 1][i]
			count += min(now.count('0'), now.count('1'))
	print(count)


def chan():
	s = 0
	for i in range(n // 2):
		for j in range(n // 2):
			g = int(a[i][j]) + int(a[j][n - i - 1]) + int(a[n - i - 1][n - j - 1]) + int(a[n - j - 1][i])
			s += min(g, 4 - g)
	return s

def le():
	s = 0
	for i in range(n // 2):
		for j in range(n // 2 + 1):
			g = int(a[i][j]) + int(a[j][n - i - 1]) + int(a[n - i - 1][n - j - 1]) + int(a[n - j - 1][i])
			s += min(g, 4 - g)
	return s
t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append(input())
	if n % 2:
		print(le())
	else:
		print(chan())


t = int(input())
for qqq in range(t):
	n = int(input())
	matr2 = list()
	for str1 in range(n):
		str2 = list(map(int, list(input())))
		matr2.append(str2)
	n2 = n // 2
	sumResult = 0
	for i in range(n2):
		for j in range(n2):
			sum2 = matr2[i][j] + matr2[j][n - i - 1] + matr2[n - i - 1][n - j - 1] + matr2[n - j - 1][i]
			if sum2 == 3:
				sum2 = 1
			elif sum2 == 4:
				sum2 = 0
			sumResult = sumResult + sum2
	if n % 2 != 0:
		for i in range(n2):
			sum2 = matr2[i][n2] + matr2[n2][i] + matr2[n2][n - i - 1] + matr2[n - i - 1][n2]
			if sum2 == 3:
				sum2 = 1
			elif sum2 == 4:
				sum2 = 0
			sumResult = sumResult + sum2
	print(sumResult)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(input())
	cnt = 0
	for i in range(0, n - 1):
		for j in range(i, n - 1 - i):
			x = int(arr[i][j]) + int(arr[j][n - 1 - i]) + int(arr[n - 1 - i][n - 1 - j]) + int(arr[n - 1 - j][i])
			if x == 1:
				cnt += 1
			elif x == 2:
				cnt += 2
			elif x == 3:
				cnt += 1
	print(cnt)


def solution(a, n):
	answer = 0
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			total = a[i][j] + a[n - 1 - i][n - 1 - j] + a[j][n - 1 - i] + a[n - 1 - j][i]
			answer += min(total, 4 - total)
	return answer
t = int(input())
answer = [0] * t
for i in range(t):
	n = int(input())
	a = [None] * n
	for j in range(n):
		a[j] = list(map(int, list(input())))
	answer[i] = solution(a, n)
print(*answer, sep='\n')


for _ in range(int(input())):
	n = int(input())
	g = [''] + ['0' + input() for i in range(n)]
	ans = 0
	for i in range(1, n // 2 + 1):
		for j in range(1, n // 2 + 1):
			(x, y) = (0, 0)
			if g[i][j] == '0':
				x += 1
			else:
				y += 1
			if g[j][n - i + 1] == '0':
				x += 1
			else:
				y += 1
			if g[n - i + 1][n - j + 1] == '0':
				x += 1
			else:
				y += 1
			if g[n - j + 1][i] == '0':
				x += 1
			else:
				y += 1
			ans += min(x, y)
	if n % 2 == 1:
		for i in range(1, n // 2 + 1):
			(x, y) = (0, 0)
			if g[i][n // 2 + 1] == '0':
				x += 1
			else:
				y += 1
			if g[n // 2 + 1][n - i + 1] == '0':
				x += 1
			else:
				y += 1
			if g[n - i + 1][n // 2 + 1] == '0':
				x += 1
			else:
				y += 1
			if g[n // 2 + 1][i] == '0':
				x += 1
			else:
				y += 1
			ans += min(x, y)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [input() for _ in range(n)]
	operation = 0
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			x = (a[i][j] + a[n - i - 1][n - j - 1] + a[j][n - i - 1] + a[n - j - 1][i]).count('1')
			operation += min(x, 4 - x)
	print(operation)


for iota in range(int(input())):
	n = int(input())
	grid = []
	ans = 0
	for i in range(n):
		grid.append(list(map(int, list(input()))))
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			num1 = grid[i][j] + grid[j][-i - 1] + grid[-j - 1][i] + grid[-i - 1][-j - 1]
			if num1 >= 2:
				ans += 4 - num1
			else:
				ans += num1
	print(ans)


def main():
	for x in range(int(input())):
		n = int(input())
		mat = []
		for i in range(n):
			mat.append(list(map(int, list(input()))))
		print(contador(mat))

def contador(mat):
	cont = 0
	for x in range(len(mat) // 2):
		for j in range(x, len(mat) - x - 1):
			u = 0
			c = 0
			if mat[x][j] == 1:
				u += 1
			else:
				c += 1
			if mat[j][len(mat) - 1 - x] == 1:
				u += 1
			else:
				c += 1
			if mat[len(mat) - 1 - x][len(mat) - j - 1] == 1:
				u += 1
			else:
				c += 1
			if mat[len(mat) - 1 - j][x] == 1:
				u += 1
			else:
				c += 1
			if c < u:
				cont += c
			else:
				cont += u
	return cont
main()


for _ in range(int(input())):
	n = int(input())
	matrix = []
	for _ in range(n):
		matrix.append(list(map(int, input())))
	answer = 0
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			temp = matrix[i][j] + matrix[j][~i] + matrix[~i][~j] + matrix[~j][i]
			answer += min(temp, 4 - temp)
	print(answer)


for _ in range(int(input())):
	n = int(input())
	d = [list(map(int, input())) for _ in range(n)]
	counter = 0
	i = 0
	while i < (n - 1) / 2:
		j = i
		while j < n - i - 1:
			summa = d[j][i] + d[n - i - 1][j] + d[n - j - 1][n - i - 1] + d[i][n - j - 1]
			if summa == 2:
				counter += 2
			if summa == 1 or summa == 3:
				counter += 1
			j += 1
		i += 1
	print(counter)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	b = [[]]
	ans = 0
	for i in range(n):
		b.append([0] + list(map(int, list(sys.stdin.readline().strip()))))
	for i in range(1, (n + 1) // 2 + 1):
		for j in range(1, n // 2 + 1):
			s = b[i][j] + b[j][n - i + 1] + b[n - i + 1][n - j + 1] + b[n - j + 1][i]
			ans += min(s, 4 - s)
	print(ans)


def nchanges(a, b, c, d):
	s = a + b + c + d
	if s >= 2:
		return 4 - s
	else:
		return s

def main():
	i = int(input())
	for _ in range(i):
		n = int(input().strip('\r\n'))
		a = []
		for _ in range(n):
			a.append([int(c) for c in input().strip('\r\n')])
		res = 0
		for iidx in range(n // 2):
			for jidx in range(n // 2):
				v1 = a[iidx][jidx]
				v2 = a[jidx][n - iidx - 1]
				v3 = a[n - iidx - 1][n - jidx - 1]
				v4 = a[n - jidx - 1][iidx]
				res += nchanges(v1, v2, v3, v4)
		if n % 2 == 1:
			for iidx in range(n // 2):
				v1 = a[n // 2][iidx]
				v2 = a[iidx][n // 2]
				v3 = a[n // 2][n - iidx - 1]
				v4 = a[n - iidx - 1][n // 2]
				res += nchanges(v1, v2, v3, v4)
		print(res)
main()


import sys
import math
from itertools import combinations
iput = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	dat = [list(input()) for i in range(n)]
	ans = 0
	for i in range(len(dat) // 2):
		for j in range(i, len(dat) - i - 1):
			(o, f) = (0, 0)
			if dat[i][j] == '0':
				o += 1
			else:
				f += 1
			if dat[j][n - i - 1] == '0':
				o += 1
			else:
				f += 1
			if dat[n - i - 1][n - j - 1] == '0':
				o += 1
			else:
				f += 1
			if dat[n - j - 1][i] == '0':
				o += 1
			else:
				f += 1
			if o == 0 or f == 0:
				ans += 0
			else:
				ans += min(o, f)
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	iter = math.ceil(n / 2)
	iter2 = n // 2
	mat = []
	res = 0
	for x in range(n):
		mat.append(input())
	for i in range(iter):
		for j in range(iter2):
			a = int(mat[i][j]) + int(mat[j][n - i - 1]) + int(mat[n - 1 - i][n - 1 - j]) + int(mat[n - 1 - j][i])
			if a == 2:
				res += 2
			elif a % 2:
				res += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = [list(map(int, input())) for _ in range(n)]
	total = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			s = a[i][j] + a[j][n - 1 - i] + a[n - 1 - i][n - 1 - j] + a[n - 1 - j][i]
			total += min(s, 4 - s)
	print(total)


t = int(input())
for vvod in range(t):
	n = int(input())
	v = list()
	ans = 0
	for i in range(n):
		s = input()
		v.append(s)
	for i in range(0, n // 2):
		for j in range(i, n - i - 1):
			g = int(v[i][j]) + int(v[n - 1 - j][i]) + int(v[n - 1 - i][n - 1 - j]) + int(v[j][n - 1 - i])
			if g == 3 or g == 1:
				ans += 1
			elif g == 2:
				ans += 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	m = [[int(x) for x in input()] for _ in range(n)]
	s = 0
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			a = m[i][j] + m[j][n - i - 1] + m[n - i - 1][n - j - 1] + m[n - j - 1][i]
			s += min(a, 4 - a)
	print(s)


import os
import collections
import math
import sys
T = int(input())
for _ in range(T):
	n = int(input())
	A = []
	for i in range(n):
		s = input()
		A.append(s)
	r = int(n / 2)
	sum = 0
	for i in range(r):
		for j in range(r):
			num0 = 0
			num1 = 0
			tmp1 = A[i][j]
			tmp2 = A[j][n - 1 - i]
			tmp3 = A[n - 1 - j][i]
			tmp4 = A[n - 1 - i][n - 1 - j]
			if tmp1 == '0':
				num0 += 1
			else:
				num1 += 1
			if tmp2 == '0':
				num0 += 1
			else:
				num1 += 1
			if tmp3 == '0':
				num0 += 1
			else:
				num1 += 1
			if tmp4 == '0':
				num0 += 1
			else:
				num1 += 1
			sum += min(num0, num1)
	if n & 1 == 1:
		for j in range(r):
			num0 = 0
			num1 = 0
			tmp1 = A[r][j]
			tmp2 = A[j][n - 1 - r]
			tmp3 = A[n - 1 - j][r]
			tmp4 = A[n - 1 - r][n - 1 - j]
			if tmp1 == '0':
				num0 += 1
			else:
				num1 += 1
			if tmp2 == '0':
				num0 += 1
			else:
				num1 += 1
			if tmp3 == '0':
				num0 += 1
			else:
				num1 += 1
			if tmp4 == '0':
				num0 += 1
			else:
				num1 += 1
			sum += min(num0, num1)
	print(sum)


for i in range(int(input())):
	n = int(input())
	l = []
	a = 0
	for i in range(n):
		l.append(list(input()))
	for i in range(n // 2 + n % 2):
		for j in range(n // 2):
			cn = int(l[i][j]) + int(l[j][~i]) + int(l[~j][i]) + int(l[~i][~j])
			a += min(cn, 4 - cn)
	print(a)


for _ in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(input())
	ans = 0
	for r in range(n // 2):
		for c in range(n // 2 + 1 if n % 2 else n // 2):
			curr = int(matrix[r][c]) + int(matrix[n - 1 - c][r]) + int(matrix[n - 1 - r][n - 1 - c]) + int(matrix[c][n - 1 - r])
			ans += min(curr, 4 - curr)
	print(ans)


for cases in range(int(input())):
	k = 0
	n = int(input())
	l = []
	for i in range(n):
		l.append(list(map(int, list(input()))))
	for i in range(n // 2 + 1):
		for j in range(i, n - i - 1):
			s = l[i][j] + l[abs(n - j - 1)][i] + l[abs(n - i - 1)][abs(n - j - 1)] + l[j][abs(n - i - 1)]
			if s == 4:
				k += 0
			elif s == 3:
				k += 1
			elif s == 2:
				k += 2
			elif s == 1:
				k += 1
			elif s == 0:
				k += 0
	print(k)


def miis():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	print
	s = [list(map(int, list(input()))) for i in range(n)]
	ans = 0
	for i in range((n - 1) // 2 + 1 - n % 2):
		for j in range((n - 1) // 2 + 1):
			ans += 2 - abs(2 - (s[i][j] + s[j][n - i - 1] + s[n - i - 1][n - j - 1] + s[n - j - 1][n - (n - i - 1) - 1]))
	print(ans)


t = int(input())
for tt in range(t):
	n = int(input())
	a = [input() for i in range(n)]
	z = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			s = int(a[i][j]) + int(a[j][n - 1 - i]) + int(a[n - 1 - i][n - 1 - j]) + int(a[n - 1 - j][i])
			z += min(s, 4 - s)
	print(z)


from collections import *
import math
for _ in range(int(input())):
	n = int(input())
	matrix = []
	for _ in range(n):
		matrix.append(list(map(int, list(input()))))
	r = len(matrix)
	c = len(matrix[0])
	ans = 0
	for i in range(0, math.ceil(r // 2)):
		for ii in range(0, math.ceil(c / 2)):
			ct = matrix[i][ii] + matrix[ii][-i - 1] + matrix[-i - 1][-ii - 1] + matrix[-ii - 1][i]
			ans += min(ct, 4 - ct)
	print(ans)


import sys
printf = sys.stdout.write

def intput():
	return int(sys.stdin.readline())

def get_int():
	return map(int, sys.stdin.readline().strip().split())

def get_lis():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_liss():
	return sys.stdin.readline().split()

def get_ints():
	return sys.stdin.readline().split()

def get_str():
	return sys.stdin.readline().strip()
from collections import defaultdict
t = intput()
while t:
	n = intput()
	s = 0
	ans = 0
	l = [input() for _ in range(n)]
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			s = int(l[i][j]) + int(l[j][n - 1 - i]) + int(l[n - 1 - i][n - 1 - j]) + int(l[n - 1 - j][i])
			ans += min(s, 4 - s)
	printf(str(ans))
	printf('\n')
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	op = 0
	for i in range(n):
		a.append(list(map(int, list(input()))))
	for i in range(n - 1):
		for j in range(i, n - i - 1):
			one = a[i][j] + a[j][n - i - 1] + a[n - i - 1][n - j - 1] + a[n - j - 1][i]
			zero = 4 - one
			if one >= zero:
				op += zero
			else:
				op += one
	print(op)


t = int(input())
for _ in range(t):
	n = int(input())
	grid = []
	for _ in range(n):
		grid.append(list(map(int, input())))
	ans = 0
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			s = grid[j][n - 1 - i] + grid[n - 1 - j][i] + grid[i][j] + grid[n - i - 1][n - j - 1]
			ans += min(s, 4 - s)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [input() for _ in range(n)]
	res = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			v = int(a[i][j]) + int(a[j][n - 1 - i]) + int(a[n - 1 - i][n - 1 - j]) + int(a[n - 1 - j][i])
			res += min(v, 4 - v)
	print(res)


def check(a, i, j):
	i1 = i + 1
	j1 = j + 1
	c = int(a[i1 - 1][j1 - 1]) + int(a[j1 - 1][n - i1]) + int(a[n - i1][n - j1]) + int(a[n - j1][i1 - 1])
	if c == 0 or c == 4:
		return 0
	elif c == 1 or c == 3:
		return 1
	elif c == 2:
		return 2
for _ in range(int(input())):
	n = int(input())
	a = [['0' for x in range(n)] for y in range(n)]
	r = 0
	for i in range(n):
		a[i] = list(input())
	for i in range((n + 2) // 2):
		if i > n - (i + 1):
			break
		elif i == n - (i + 1):
			r += 0
		else:
			for j in range(i, n - (i + 1)):
				r += check(a, i, j)
	print(r)


def rasn(*l):
	_s = sum(map(int, l))
	if _s > 2:
		return 4 - _s
	else:
		return _s
for _ in range(int(input())):
	n = int(input())
	ls = [input() for _ in range(n)]
	w = n // 2
	h = w + n % 2
	r = 0
	for i in range(w):
		for j in range(h):
			r += rasn(ls[j][i], ls[i][-j - 1], ls[-j - 1][-i - 1], ls[-i - 1][j])
	print(r)


for _ in range(int(input())):
	n = int(input())
	lst = []
	for i in range(n):
		lst.append(input())
	ans = int()
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			s = int(lst[i][j]) + int(lst[j][n - 1 - i]) + int(lst[n - 1 - j][i]) + int(lst[n - 1 - i][n - 1 - j])
			ans += abs(min(s, 4 - s))
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [list(map(int, list(input()))) for _ in range(n)]
	tot = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			vs = [s[i][j], s[j][~i], s[~i][~j], s[~j][i]]
			tot += min(sum(vs), 4 - sum(vs))
	print(tot)


for _ in range(int(input())):
	n = int(input())
	ls = [input() for i in range(n)]
	ans = 0
	for i in range(0, n // 2):
		for j in range(i, n - 1 - i):
			p1 = n - 1 - i
			p2 = n - 1 - j
			l = [ls[i][j], ls[p1][p2], ls[p2][i], ls[j][p1]]
			(z, o) = (0, 0)
			for c in l:
				if c == '0':
					o += 1
				else:
					z += 1
			ans += min(o, z)
	print(ans)


for nothing in range(int(input())):
	ans = 0
	n = int(input())
	m = [[int(j) for j in input()] for i in range(n)]
	for i in range(n // 2):
		for j in range(n // 2):
			s = m[i][j] + m[j][n - 1 - i] + m[n - 1 - i][n - 1 - j] + m[n - 1 - j][i]
			ans += min(4 - s, s)
	if n % 2 == 1:
		for i in range(n // 2):
			s = m[n // 2][i] + m[n // 2][n - 1 - i] + m[n - 1 - i][n // 2] + m[i][n // 2]
			ans += min(4 - s, s)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	m = list()
	for i in range(n):
		m.append(list(map(int, input())))
	res = 0
	for i in range(n // 2 + 1):
		for j in range(i, n - i - 1):
			fingerprint = sum([m[i][j], m[n - j - 1][i], m[n - i - 1][n - j - 1], m[j][n - i - 1]])
			res += min(fingerprint, 4 - fingerprint)
	print(res)


import sys
import math
input = sys.stdin.readline
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	lst = []
	for i in range(n):
		lst.append(list(str(input()))[:-1])
	k = (n + 1) // 2
	t = 0
	ans = []
	while t < k:
		p = math.ceil(n / 2)
		i = t
		for j in range(n // 2):
			ans.append(int(lst[i][j]) + int(lst[n - j - 1][i]) + int(lst[n - i - 1][n - j - 1]) + int(lst[j][n - i - 1]))
		t += 1
	dem = 0
	for i in ans:
		dem += min(i, 4 - i)
	print(dem)


for o in range(int(input())):
	x = int(input())
	m = []
	for i in range(x):
		l = input()
		m.append(l)
	c = 0
	n = x - 1
	for i in range(x // 2):
		(p, q) = (i, x - (i + 1))
		for j in range(n):
			s = 0
			s += int(m[p][p + j]) + int(m[p + j][q]) + int(m[q][q - j]) + int(m[q - j][p])
			if 0 < s < 4:
				if s < 4 - s:
					c += s
				else:
					c += 4 - s
		n -= 2
	print(c)


for _ in range(int(input())):
	n = int(input())
	M = [list(map(int, input())) for __ in range(n)]
	count = 0
	for col in range((n + 1) // 2):
		for row in range(n // 2):
			ones = int(M[row][col]) + int(M[col][-1 - row]) + int(M[-1 - row][-1 - col]) + int(M[-1 - col][row])
			count += min(ones, 4 - ones)
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	grid = [[] for j in range(n)]
	for j in range(n):
		row = input()
		for k in range(n):
			grid[j].append(row[k])
	ops = 0
	if n % 2 == 0:
		r2 = n // 2
	else:
		r2 = n // 2 + 1
	for j in range(0, n // 2):
		for k in range(0, r2):
			m = 1
			if grid[j][k] == grid[n - 1 - k][j]:
				m += 1
			if grid[j][k] == grid[n - 1 - j][n - 1 - k]:
				m += 1
			if grid[j][k] == grid[k][n - 1 - j]:
				m += 1
			if m == 1 or m == 3:
				ops += 1
			elif m == 2:
				ops += 2
	print(ops)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	mas = []
	ans = 0
	for jj in range(n):
		a1 = [int(ii) for ii in input()]
		mas.append(a1)
	a = 0
	nn = n
	while nn > 1:
		for i in range(nn - 1):
			temp = mas[a][i + a] + mas[a + i][n - 1 - a] + mas[n - 1 - a][n - 1 - a - i] + mas[n - 1 - a - i][a]
			if temp == 3:
				temp = 1
			elif temp == 4:
				temp = 0
			ans += temp
		a += 1
		nn -= 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		temp = [int(i) for i in input()]
		arr.append(temp)
	res = 0
	for i in range(n // 2):
		for j in range(i, n - 1 - i):
			cnt = 0
			cnt += arr[i][j]
			cnt += arr[j][n - 1 - i]
			cnt += arr[n - 1 - i][n - 1 - j]
			cnt += arr[n - 1 - j][i]
			res += min(cnt, 4 - cnt)
	print(res)


def read_int() -> int:
	return int(input().strip())

def read_ints_map() -> map:
	return map(int, input().strip().split())

def read_ints_list() -> [int]:
	return [*read_ints_map()]
try:
	fin = open('./input.txt')
	import sys
	sys.stdin = fin
except:
	pass
t = read_int()
for _ in range(t):
	n = read_int()
	mat = []
	for _ in range(n):
		mat.append(input())
	mid = n >> 1
	moves = 0
	for i in range(mid):
		for j in range(mid):
			s = mat[i][j] + mat[j][n - 1 - i] + mat[n - 1 - i][n - 1 - j] + mat[n - 1 - j][i]
			moves += min(s.count('0'), s.count('1'))
	if n & 1:
		for i in range(mid):
			s = mat[i][mid] + mat[n - 1 - i][mid] + mat[mid][i] + mat[mid][n - 1 - i]
			moves += min(s.count('0'), s.count('1'))
	print(moves)


def main():
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		g = [[0 for _ in range(n)] for _ in range(n)]
		for i in range(n):
			line = input()
			for (j, c) in enumerate(line):
				g[i][j] = c
		count_one = 0
		count_zero = 0
		count = 0
		for i in range(0, n // 2):
			for j in range(i, n - i - 1):
				count_one = int(g[j][n - 1 - i]) + int(g[n - 1 - i][n - 1 - j]) + int(g[i][j]) + int(g[n - 1 - j][i])
				count_zero = 4 - count_one
				count += min(count_one, count_zero)
		print(count)
main()


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		l.append(input())
	if n % 2 == 1:
		x = n // 2
		ans = 0
		for i in range(x + 1):
			for j in range(1, x + 1):
				if i != 0 or j != 0:
					b = l[x - i][x - j] + l[x + j][x - i] + l[x + i][x + j] + l[x - j][x + i]
					a = 0
					for k in b:
						if k == '1':
							a += 1
					ans += min(abs(a), abs(4 - a))
	if n % 2 == 0:
		x = n // 2
		ans = 0
		for i in range(x):
			for j in range(x):
				b = l[i][j] + l[n - 1 - j][i] + l[n - 1 - i][n - 1 - j] + l[j][n - 1 - i]
				a = 0
				for k in b:
					if k == '1':
						a += 1
				ans += min(abs(a), abs(4 - a))
	print(ans)


def resiOmotac(matrica, duzina, pocI, pocJ):
	if duzina == 1:
		return 0
	jedinice = [0] * (duzina - 1)
	for i in range(duzina - 1):
		jedinice[i] += matrica[pocI][pocJ + i]
	for i in range(duzina - 1):
		jedinice[i] += matrica[pocI + i][pocJ + duzina - 1]
	for i in range(duzina - 1):
		jedinice[i] += matrica[pocI + duzina - 1][pocJ + duzina - 1 - i]
	for i in range(duzina - 1):
		jedinice[i] += matrica[pocI + duzina - 1 - i][pocJ]
	iz = 0
	for i in range(duzina - 1):
		if jedinice[i] > 2:
			iz += 4 - jedinice[i]
		else:
			iz += jedinice[i]
	return iz

def resi():
	n = int(input())
	matrica = [[0] * n for i in range(n)]
	for i in range(n):
		s = str(input())
		for j in range(n):
			matrica[i][j] = int(s[j])
	resenje = 0
	for i in range(n // 2):
		resenje += resiOmotac(matrica, n - 2 * i, i, i)
	print(resenje)
for _ in range(int(input())):
	resi()


import math
for _ in range(int(input())):
	a = int(input())
	l = []
	c = 0
	m = a - 1
	n = m
	for i in range(a):
		l.append(input())
	for i in range(math.ceil(a / 2)):
		for j in range(i, n):
			w = int(l[i][j])
			x = int(l[m - i][m - j])
			y = int(l[j][m - i])
			z = int(l[m - j][i])
			s = w + x + y + z
			c += min(s, 4 - s)
		n -= 1
	print(c)


import math

def resolve(N, list_1):
	result = 0
	if N % 2 == 0:
		X = int(N / 2)
		Y = int(N / 2)
	else:
		X = int(N / 2) + 1
		Y = int(N / 2)
	for x_index in range(X):
		for y_index in range(Y):
			a = list_1[x_index][y_index]
			b = list_1[y_index][N - 1 - x_index]
			c = list_1[N - 1 - x_index][N - 1 - y_index]
			d = list_1[N - 1 - y_index][x_index]
			one_num = 0
			zero_num = 0
			if a == 1:
				one_num += 1
			else:
				zero_num += 1
			if b == 1:
				one_num += 1
			else:
				zero_num += 1
			if c == 1:
				one_num += 1
			else:
				zero_num += 1
			if d == 1:
				one_num += 1
			else:
				zero_num += 1
			smaller = min(zero_num, one_num)
			result += smaller
	return result

def read_input():
	N = int(input())
	list_1 = []
	for index in range(N):
		list_2 = []
		temp_2 = input()
		for item in temp_2:
			list_2.append(int(item))
		list_1.append(list_2)
	return (N, list_1)
case_num = int(input())
if not case_num == 0:
	for index in range(case_num):
		(N, list_1) = read_input()
		res = resolve(N, list_1)
		print(res)


from math import ceil

def rotate_matrix(m):
	return [[m[j][i] for j in range(len(m))] for i in range(len(m[0]) - 1, -1, -1)]
n = int(input())
for i in range(n):
	k = int(input())
	arr = []
	for j in range(k):
		brr = list(input())
		arr.append(brr)
		sum = 0
	for x in range(0, k // 2):
		for y in range(0 + x, k - 1 - x):
			s1 = arr[x][k - y - 1]
			s2 = arr[y][x]
			s3 = arr[k - y - 1][k - x - 1]
			s4 = arr[k - x - 1][y]
			temp = int(s1) + int(s2) + int(s3) + int(s4)
			sum += min(temp, 4 - temp)
	print(ceil(sum))


from math import ceil

def main():
	temp = int(int(input()))
	for _ in range(temp):
		l = int(input())
		m = [list(map(int, list(input()))) for i in range(l)]
		a = (l + 1) // 2
		k = 0
		for i in range(a):
			for j in range(a):
				k_0 = 0
				k_1 = 0
				if m[i][j] == 1:
					k_1 += 1
				else:
					k_0 += 1
				if m[l - j - 1][i] == 1:
					k_1 += 1
				else:
					k_0 += 1
				if m[l - i - 1][l - j - 1] == 1:
					k_1 += 1
				else:
					k_0 += 1
				if m[j][l - i - 1] == 1:
					k_1 += 1
				else:
					k_0 += 1
				if k_0 > k_1:
					k += k_1
					m[i][j] = 1
					m[l - j - 1][i] = 1
					m[l - i - 1][l - j - 1] = 1
					m[j][l - i - 1] = 1
				else:
					k += k_0
					m[i][j] = 0
					m[l - j - 1][i] = 0
					m[l - i - 1][l - j - 1] = 0
					m[j][l - i - 1] = 0
		print(k)
main()


t = int(input())
while t:
	n = int(input())
	grid = []
	for i in range(n):
		grid.append(list(map(int, list(input()))))
	ans = 0
	for i in range(int((n + 1) / 2)):
		for j in range(int(n / 2)):
			r = i
			c = j
			s = grid[r][c]
			r = n - 1 - r
			s += grid[c][r]
			c = n - 1 - c
			s += grid[r][c]
			r = i
			s += grid[c][r]
			ans = ans + min(4 - s, s)
	print(ans)
	t = t - 1


from collections import defaultdict as df
import bisect
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		s = input()
		a.append(s)
	minop = 0
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			count = int(a[i][j])
			count += int(a[n - i - 1][n - j - 1])
			count += int(a[j][n - i - 1])
			count += int(a[n - j - 1][i])
			minop += min(count, 4 - count)
	print(minop)


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	count = 0
	d = {}
	for i in range(n // 2 + 1):
		for j in range(i, n - i - 1):
			d[i, j, 1] = int(l[i][j]) + int(l[j][n - i - 1]) + int(l[n - i - 1][n - j - 1]) + int(l[n - j - 1][i])
	for i in d:
		count += min(d[i], 4 - d[i])
	print(count)


for t in range(int(input())):
	n = int(input())
	matrix = [list(map(int, list(input()))) for x in range(n)]
	check = n // 2
	count = 0
	for i in range(check):
		pos_ul = [i, i]
		pos_dl = [n - 1 - i, i]
		pos_ur = [i, n - 1 - i]
		pos_dr = [n - 1 - i, n - 1 - i]
		for k in range(n - 1 - 2 * i):
			nums = matrix[pos_ul[0]][pos_ul[1]] + matrix[pos_dl[0]][pos_dl[1]] + matrix[pos_ur[0]][pos_ur[1]] + matrix[pos_dr[0]][pos_dr[1]]
			count += min(nums, 4 - nums)
			pos_ul[1] += 1
			pos_dl[0] -= 1
			pos_ur[0] += 1
			pos_dr[1] -= 1
	print(count)


tt = int(input())
for _ in range(tt):
	n = int(input())
	l = []
	for i in range(n):
		s = input()
		temp = [int(i) for i in list(s)]
		l.append(temp)
	c = 0
	for i in range(n // 2):
		for j in range((n + 1) // 2):
			zero = l[i][j]
			ninety = l[j][n - i - 1]
			rev = l[n - i - 1][n - j - 1]
			twoseven = l[n - j - 1][i]
			s = zero + ninety + rev + twoseven
			c += min(s, 4 - s)
	print(c)


t = int(input())
for i in range(t):
	ar = []
	n = int(input())
	for j in range(n):
		tmp = [int(k) for k in list(input())]
		ar.append(tmp)
	mid = n // 2
	ans = 0
	for j in range(mid):
		u = [ar[j][k] for k in range(j, n - j)]
		r = [ar[k][n - j - 1] for k in range(j, n - j)]
		d = [ar[n - j - 1][k] for k in range(j, n - j)]
		l = [ar[k][j] for k in range(j, n - j)]
		for k in range(n - 2 * j - 1):
			sum = u[k] + r[k] + d[len(d) - 1 - k] + l[len(l) - 1 - k]
			ans += min(sum, 4 - sum)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	table = []
	for i in range(n):
		table.append(list(map(int, input())))
	for i in range((n + 1) // 2):
		for j in range(n // 2):
			s = [table[i][j], table[j][n - 1 - i], table[n - 1 - i][n - j - 1], table[n - j - 1][i]]
			sm = sum(s)
			ans += min(sm, 4 - sm)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(list(map(int, list(input()))))
	ans = 0
	for i in range(len(matrix) // 2):
		for j in range(i, len(matrix) - i - 1):
			sum_val = matrix[i][j]
			sum_val += matrix[len(matrix) - 1 - j][i]
			sum_val += matrix[len(matrix) - 1 - i][len(matrix) - 1 - j]
			sum_val += matrix[j][len(matrix) - 1 - i]
			ans += abs(abs(sum_val - 2) - 2)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	grid = []
	for i in range(n):
		grid.append([int(a) for a in input()])
	count = 0
	for i in range(n // 2):
		for j in range(n // 2):
			s = grid[i][j] + grid[j][n - i - 1] + grid[n - i - 1][n - j - 1] + grid[n - j - 1][i]
			count += min(s, 4 - s)
	if n % 2:
		for i in range(n // 2):
			s = grid[i][n // 2] + grid[n // 2][n - i - 1] + grid[n - i - 1][n // 2] + grid[n // 2][i]
			count += min(s, 4 - s)
	print(count)