← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var t int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &t)

	for k := 0; k < t; k++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		grid := make([][]int, n)
		for i := 0; i < n; i++ {
			grid[i] = make([]int, m)
			for j := 0; j < m; j++ {
				fmt.Fscan(reader, &grid[i][j])
			}
		}

		totalChanges := 0
		totalDist := n + m - 2
		
		// We iterate through distances from 0 to totalDist / 2 - 1
		// For each distance d, the symmetric distance is totalDist - d
		// We treat the set of cells at distance d and totalDist - d as one group
		// that must have the same value.
		for d := 0; d < totalDist/2; d++ {
			cnt0 := 0
			cnt1 := 0

			// Process cells at distance d
			// Cells (r, c) satisfy r + c = d + 2 (using 1-based indexing)
			// Iterate over all valid rows r
			for r := 1; r <= n; r++ {
				c := d + 2 - r
				if c >= 1 && c <= m {
					if grid[r-1][c-1] == 0 {
						cnt0++
					} else {
						cnt1++
					}
				}
			}

			// Process cells at the symmetric distance totalDist - d
			d2 := totalDist - d
			for r := 1; r <= n; r++ {
				c := d2 + 2 - r
				if c >= 1 && c <= m {
					if grid[r-1][c-1] == 0 {
						cnt0++
					} else {
						cnt1++
					}
				}
			}

			if cnt0 < cnt1 {
				totalChanges += cnt0
			} else {
				totalChanges += cnt1
			}
		}

		// If totalDist is odd, there is a middle layer that pairs with itself.
		// Since x == x is always true, no changes are needed for the middle layer.
		
		fmt.Println(totalChanges)
	}
}
```