8. Given a positive integer $n \geqslant 2$, let the square grid be
$$
\begin{array}{l}
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),
\end{array}
$$

satisfying $\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\} \text {. }
$$

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_{i j}=(i-1) n+j \text {. }
$$

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 only experienced 1 swap, 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 $2 n-1$ swaps.

If the columns containing $1,2, \cdots, n$ are all different, then we can move them to the first row of $A$ with at most $n$ swaps, and then transform the first row of $A$ 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 (say the $k$-th column) that does not contain any of $1,2, \cdots, n$. Move $k$ to the $k$-th column, and then to the first row, without changing the positions of $1, \cdots, k-1, k+1, \cdots, n$.

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

Similarly, we can transform the $i$-th row of $A$ ($i=2,3, \cdots, n-1$) into
$((i-1) n+1, (i-1) n+2, \cdots, i n)$ with at most $2 n-1$ swaps.
Thus, we can transform the $n$-th row of $A$ into
$$
\left((n-1) n+1, (n-1) n+2, \cdots, n^{2}\right)
$$
with at most $n-1$ swaps.

In total, we need at most $2 n(n-1)$ swaps.
Finally, we prove: $m=2 n(n-1)$ is the required number.
$$
\text { Let } A=\left(\begin{array}{ccccc}
n+2 & n+3 & \cdots & 2 n & n+1 \\
2 n+2 & 2 n+3 & \cdots & 3 n & 2 n+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) \text {. }
$$

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.