12. Let $n$ be a positive integer, and call the intersection points of the grid lines of an $n \times n$ square grid $T_{n}$ (a total of $(n+1)^{2}$ points) as lattice points. Now, assign the numbers $1,2, \cdots,(n+1)^{2}$ to all the lattice points of $T_{n}$, such that different lattice points receive different numbers. Call a $1 \times 1$ square $S$ in $T_{n}$ a "good square" if, starting from one of its vertices and reading the 4 vertices in a counterclockwise direction, the numbers on these vertices are in increasing order (the figure shows one way to assign the numbers $1,2, \cdots, 9$ to the lattice points of $T_{2}$, where $B, C$ are good squares, and $A, D$ are not good squares). Let the maximum number of good squares in $T_{n}$ be $f(n)$.
(1) Find the value of $f(2)$;
(2) Find the expression for $f(n)$ in terms of the positive integer $n$.
(1) As shown in the figure, the 4 $1 \times 1$ cells (hereinafter referred to as cells) of $T_{2}$ are denoted as $A, B, C, D$, and the 9 grid points are denoted as $a, b, c, d, e, f, g, h, i$. When $a, b, \cdots, i$ are sequentially $1, 2, \cdots, 9$, it is easy to verify that $B, C, D$ are all good cells, i.e., $f(2) \geqslant 3$.
Now assume $f(2)=4$, then there exists a way to assign the numbers such that $A, B, C, D$ are all good cells. By symmetry, without loss of generality, assume the smallest number among the 8 boundary points $a, b, \cdots, h$ is $a$ or $b$. Since $A$ is a good cell, we have $a < b < i < h$ or $b < i < h < a$, so $b < i < h$ always holds. Furthermore, since $B, C$ are good cells, we have $i < f < g < h, b < c < d < i \Rightarrow d < i < f$, which contradicts $D$ being a good cell. Therefore, $f(2)=3$.
(2) Suppose the numbers on the grid points of $T_{n}$ have been assigned, and at this point there are $k$ good cells. We call a side of a cell a segment, and we mark an arrow on each segment of $T_{n}$: if a segment connects two grid points $U, V$, where the number on $U$ is less than the number on $V$, then we mark $\overrightarrow{U V}$ on the segment $U V$. We call an ordered pair $(S, U V)$, where $S$ is a cell and $U V$ is a segment of $S$, a pair if $\overrightarrow{U V}$ is in the counterclockwise direction from the center of $S$. Let the total number of pairs be $N$.
On one hand, each cell $S$ contributes at least one pair (otherwise reading the numbers on the vertices of $S$ in a counterclockwise direction will always increase, which is a contradiction), and according to the definition of a good cell, each good cell contributes 3 pairs, so $N \geqslant 3 k + 1 \cdot (n^2 - k) = 2 k + n^2$; on the other hand, each segment of $T_{n}$ contributes at most 1 pair, and there is at least one segment on the boundary of $T_{n}$ that has an arrow pointing in the clockwise direction from the center of the $n \times n$ grid (otherwise reading the numbers on the boundary of $T_{n}$ in a counterclockwise direction will always increase, which is a contradiction), so this segment does not contribute a pair. Note that $T_{n}$ has a total of $2 n(n+1)$ segments, so $N \leqslant 2 n(n+1) - 1$.
Therefore, $2 k + n^2 \leqslant 2 n(n+1) - 1 \Rightarrow k \leqslant \frac{n^2 + 2 n - 1}{2}$.
Finally, for the case where $n$ is odd (left figure) and $n$ is even (right figure), assign the numbers $1, 2, \cdots, (n+1)^2$ to all grid points in the order they are passed by the thick lines. For each cell marked with a $\Delta$ in the figure, it is easy to verify that arranging the 4 vertices of the cell in the order they are passed by the thick lines results in a counterclockwise arrangement, so these cells are all good cells.
The number of good cells in the left figure is $\frac{n+1}{2} \cdot n + \frac{n-1}{2} = \frac{n^2 + 2 n - 1}{2}$;
The number of good cells in the right figure is $\frac{n}{2} \cdot n + \frac{n}{2} - 1 + \frac{n}{2} = \frac{n^2 + 2 n - 2}{2}$.
In summary, $f(n) = \left[\frac{n^2 + 2 n - 1}{2}\right]$.