A $3 \times 3$ grid is to be painted with three colors: red, green, and blue, such that:
1. No two squares that share an edge are the same color.
2. No two corner squares on the same edge of the grid have the same color.

For example, the upper-left and bottom-left squares cannot both be red, as that would violate condition (ii). In how many ways can this be done? Note that rotations and reflections are considered distinct colorings.

Let's think step by step and output the final answer within \boxed{}.
user