Palindromic Paths
Contest: Educational Codeforces Round 89 (Rated for Div. 2)
Difficulty rating: 1500
Tags: greedy, math

You are given a matrix with $$$n$$$ rows (numbered from $$$1$$$ to $$$n$$$) and $$$m$$$ columns (numbered from $$$1$$$ to $$$m$$$). A number $$$a_{i, j}$$$ is written in the cell belonging to the $$$i$$$-th row and the $$$j$$$-th column, each number is either $$$0$$$ or $$$1$$$.

A chip is initially in the cell $$$(1, 1)$$$, and it will be moved to the cell $$$(n, m)$$$. During each move, it either moves to the next cell in the current row, or in the current column (if the current cell is $$$(x, y)$$$, then after the move it can be either $$$(x + 1, y)$$$ or $$$(x, y + 1)$$$). The chip cannot leave the matrix.

Consider each path of the chip from $$$(1, 1)$$$ to $$$(n, m)$$$. A path is called palindromic if the number in the first cell is equal to the number in the last cell, the number in the second cell is equal to the number in the second-to-last cell, and so on.

Your goal is to change the values in the minimum number of cells so that every path is palindromic.

Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 200$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 30$$$) — the dimensions of the matrix.

Then $$$n$$$ lines follow, the $$$i$$$-th line contains $$$m$$$ integers $$$a_{i, 1}$$$, $$$a_{i, 2}$$$, ..., $$$a_{i, m}$$$ ($$$0 \le a_{i, j} \le 1$$$).

Output format:
For each test case, print one integer — the minimum number of cells you have to change so that every path in the matrix is palindromic.

Constraints:
Time limit: 1.5 s
Memory limit: 256.0 MB

Note:
The resulting matrices in the first three test cases:

$$$\begin{pmatrix} 1 & 1\\ 0 & 1 \end{pmatrix}$$$

$$$\begin{pmatrix} 0 & 0 & 0\\ 0 & 0 & 0 \end{pmatrix}$$$

$$$\begin{pmatrix} 1 & 0 & 1 & 1 & 1 & 1 & 1\\ 0 & 1 & 1 & 0 & 1 & 1 & 0\\ 1 & 1 & 1 & 1 & 1 & 0 & 1 \end{pmatrix}$$$

Example input:
4
2 2
1 1
0 1
2 3
1 1 0
1 0 0
3 7
1 0 1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 0 1
3 5
1 0 1 0 0
1 1 1 1 0
0 0 1 0 0

Example output:
0
3
4
4

Editorial:
Let's group the cells by their distance from the starting point: the group $$$0$$$ consists of a single cell $$$(1, 1)$$$; the group $$$1$$$ consists of the cells $$$(1, 2)$$$ and $$$(2, 1)$$$, and so on. In total, there are $$$n + m - 1$$$ groups.
Let's analyze the groups $$$k$$$ and $$$n + m - 2 - k$$$. There are two cases:
• if $$$k = 0$$$ or $$$n + m - 2 - k = 0$$$, then we are looking at the starting cell and the ending cell, and their contents should be equal;
• otherwise, suppose two cells $$$(x, y)$$$ and $$$(x + 1, y - 1)$$$ belong to the same group. We can easily prove that the contents of these two cells should be equal (for example, by analyzing two paths that go through cell $$$(x + 1, y)$$$ and coincide after this cell, but one goes to $$$(x + 1, y)$$$ from $$$(x, y)$$$, and another — from $$$(x + 1, y - 1)$$$) — and, using induction, we can prove that the contents of all cells in a group should be equal. And since the paths should be palindromic, the contents of the group $$$k$$$ should be equal to the contents of the group $$$n + m - 2 - k$$$.
So, in each pair of groups, we should calculate the number of $$$1$$$'s and $$$0$$$'s, and choose which of them to change. Note that if $$$n + m$$$ is even, the central group has no pair, so it should not be modified.