5. Let $m$ be a positive integer. Consider an $m \times m$ chessboard. At the center of some unit squares, there is an ant each. At time 0, each ant moves in a direction parallel to one of the edges of the chessboard at a speed of 1. When two ants meet while moving in opposite directions, they both turn $90^{\circ}$ clockwise and continue moving at a speed of 1. When more than two ants meet or two ants meet while moving in perpendicular directions, they continue moving in their original directions. When an ant reaches the boundary of the chessboard, it falls off and does not reappear. For all possible initial positions, find the latest time at which the last ant falls off the chessboard, or prove that this time does not necessarily exist.
5. The latest moment for the last ant to fall off the chessboard is $\frac{3 m}{2}-1$.
When $m=1$, the conclusion is obviously true.
Thus, assume $m>1$.
A collision occurs when exactly two ants meet moving in opposite directions.

Initially, if one ant is placed in the unit square at the southwest corner facing east, and another ant is placed in the unit square at the southeast corner facing west, they will meet at the midpoint of the last row (i.e., the midpoint of the line segment connecting their initial positions) at time $\frac{m-1}{2}$. After the collision, one of the ants moves north and falls off the chessboard after another $m-\frac{1}{2}$ time. Therefore, the last ant falls off the chessboard at time
$$
\frac{m-1}{2}+m-\frac{1}{2}=\frac{3 m}{2}-1
$$

Now, we prove that this is the latest possible moment.
Consider any collision between two ants $a$ and $a^{\prime}$. By changing the collision rule to force these two ants to rotate counterclockwise, all movement scenarios remain unchanged, except that $a$ and $a^{\prime}$ swap positions after the collision. Applying this to any collision, we can assume that for any collision, we can choose either both ants rotate clockwise or both ants rotate counterclockwise. Therefore, we can assume there are only two types of ants (depending on their initial direction): NE-ants move only north or east, and SW-ants move only south or west.

Thus, after time $2 m-1$, all ants will fall off the chessboard. Therefore, by considering the collisions of one ant with other ants, we obtain an optimal upper bound.

Consider a Cartesian coordinate system, where the coordinates of the four corners of the chessboard are $(0,0),(m, 0),(m, m),(0, m)$. At time $t$, no NE-ant is in the region
$$
\{(x, y) \mid x+y2 m-t-1\} \text {. }
$$

If two ants collide at point $(x, y)$ at time $t$, then
$$
t+1 \leqslant x+y \leqslant 2 m-t-1 \text {. }
$$

Similarly, by changing the rule so that each ant alternates between moving only north or west, or only south or east.

Similar to equation (1), if two ants collide at point $(x, y)$ at time $t$, then
$$
|x-y| \leqslant m-t-1 \text {. }
$$

Let $B(t)$
$$
\begin{aligned}
= & \{(x, y) \mid x 、 y \in[0, m], t+1 \leqslant x+y \leqslant \\
& 2 m-t-1,|x-y| \leqslant m-t-1\} .
\end{aligned}
$$

Then, at time $t$, an ant can only collide with another ant in the region $B(t)$.

Figure 1 shows $B(t)$ for $m=6$ at $t=\frac{1}{2}$ and $t=\frac{7}{2}$.

Assume a NE-ant collides for the last time at point $(x, y)$ at time $t$ (if this ant has never collided, then since $m-\frac{1}{2}<\frac{3 m}{2}-1$, this ant has already fallen off the chessboard at time $m-\frac{1}{2}$. Thus, this case can be ignored).
Therefore, $(x, y) \in B(t)$,
$x+y \geqslant t+1, x-y \geqslant-(m-t-1)$.
Hence, $x \geqslant \frac{(t+1)-(m-t-1)}{2}=t+1-\frac{m}{2}$.
By symmetry, $y \geqslant t+1-\frac{m}{2}$.
Thus, $\min \{x, y\} \geqslant t+1-\frac{m}{2}$.
After this collision, this ant will move directly to the boundary of the chessboard, taking at most $m-\min \{x, y\}$ time. Therefore, the time this ant spends on the chessboard is at most
$$
\begin{array}{l}
t+(m-\min \{x, y\}) \\
\leqslant t+m-\left(t+1-\frac{m}{2}\right)=\frac{3 m}{2}-1 .
\end{array}
$$

By symmetry, for SW-ants, the same upper bound applies.