8. Given a positive integer $n \geqslant 2$, let the square grid
$A=\left(\begin{array}{cccc}a_{11} & a_{12} & \cdots & a_{1 n} \\ a_{21} & a_{22} & \cdots & a_{2 n} \\ \vdots & \vdots & \vdots & \vdots \\ a_{n 1} & a_{n 2} & \cdots & a_{n n}\end{array}\right)$,
$B=\left(\begin{array}{cccc}b_{11} & b_{12} & \cdots & b_{1 n} \\ b_{21} & b_{22} & \cdots & b_{2 n} \\ \vdots & \vdots & \vdots & \vdots \\ b_{n 1} & b_{n 2} & \cdots & b_{n n}\end{array}\right)$
satisfy $\left\{a_{i j} \mid 1 \leqslant i, j \leqslant n\right\}=\left\{b_{i j} \mid 1 \leqslant i, j \leqslant n\right\}=\left\{1,2, \cdots, n^{2}\right\}$.
For $A$, the following operation can be performed: select two numbers in the same row or the same column, swap their positions, and keep the other $n^{2}-2$ numbers unchanged. This operation is called a transposition. Find the smallest positive integer $m$, such that for any $A, B$, $A$ can be transformed into $B$ with no more than $m$ transpositions.
8. For any $x, y$, without loss of generality, let
$b_{ij} = (i-1)n + j$.
First, we prove two lemmas.
Lemma 1: Any permutation $\left(a_{1}, a_{2}, \cdots, a_{n}\right)$ of $(1, 2, \cdots, n)$ can be transformed into $(1, 2, \cdots, n)$ with at most $n-1$ swaps.

Proof of Lemma 1: Move $i = 1, 2, \cdots, n-1$ to position $i$.

Lemma 2: Transforming the permutation $(2, 3, \cdots, n, 1)$ into the permutation $(1, 2, \cdots, n)$ requires at least $n-1$ swaps.
Proof of Lemma 2: Use mathematical induction on $n$.
For $n=2$, the conclusion is obviously true.
Assume that $(2, 3, \cdots, n, 1)$ can be transformed into $(1, 2, \cdots, n)$ with $m (m < n-1)$ swaps. Then there exists $k \in \{2, 3, \cdots, n\}$ that has only been swapped once, i.e., from position $k-1$ to position $k$. Thus, $(2, \cdots, k-1, k+1, \cdots, n, 1)$ can be transformed into $(1, 2, \cdots, k-1, k+1, \cdots, n)$ with $m-1$ swaps, which is a contradiction.
Lemmas 1 and 2 are proved.
Next, we prove: Any two-dimensional arrangement $A$ can be transformed into having the first row as $(1, 2, \cdots, n)$ with at most $2n-1$ swaps.

If the columns containing $1, 2, \cdots, n$ are all different, then it can be transformed into having them in the first row of $A$ with at most $n$ swaps, and then the first row of $A$ can be transformed into $(1, 2, \cdots, n)$ with at most $n-1$ swaps.

If two of $1, 2, \cdots, n$ are in the same column, then there exists a column (let it be the $k$-th column) that does not contain any of $1, 2, \cdots, n$. Swap $k$ to the $k$-th column, and then swap it to the first row, without changing the positions of $1, \cdots, k-1, k+1, \cdots, n$.

Repeating the above two types of operations, the first row of $A$ can be transformed into $(1, 2, \cdots, n)$ with at most $2n-1$ swaps.

Similarly, the $i$-th row of $A$ ($i=2, 3, \cdots, n-1$) can be transformed into
$$
((i-1)n+1, (i-1)n+2, \cdots, in)
$$
with at most $2n-1$ swaps.

Thus, the $n$-th row of $A$ can be transformed into
$$
\left((n-1)n+1, (n-1)n+2, \cdots, n^2\right)
$$
with at most $n-1$ swaps.

In total, at most $2n(n-1)$ swaps are required.
Finally, we prove: $m = 2n(n-1)$ is the required number.
$$
\text{Let } A = \left(\begin{array}{ccccc}
n+2 & n+3 & \cdots & 2n & n+1 \\
2n+2 & 2n+3 & \cdots & 3n & 2n+1 \\
\vdots & \vdots & \vdots & \vdots & \vdots \\
(n-1)n+2 & (n-1)n+3 & \cdots & n^2 & (n-1)n+1 \\
2 & 3 & \cdots & n & 1
\end{array}\right).
$$

Notice that the $n$ numbers in the $i$-th column of $A$ are all in the $(i+1)$-th column of $B$.

According to Lemma 2, transforming $A$ into $B$ requires at least $n(n-1)$ horizontal swaps.

Similarly, the $n$ numbers in the $i$-th row of $A$ are all in the $(i+1)$-th row of $B$, so transforming $A$ into $B$ requires at least $n(n-1)$ vertical swaps.