This can be solved with recursion.
The Sierpinski Carpet is a self-similar pattern with 8 non-overlapping copies of itself.
It starts with a white square divided into 9 smaller subsquares, which interior square is filled with black (Depth = 1).
To obtain a carpet at Depth = 2, do the same procedure recursively to the remaining 8 subsquares.
Here is an example of the carpet with Depth 1, 2, 3, & 4.

Input contains single integer n, the Depth of the carpet (1 <= n <= 8).
Please output the Sierpiński carpet of side length 3n and use '.' to represent white, '#' to represent black.