# Problem Description
This is a grid pattern generation problem where we need to:
- Create an n×n grid (where n is always odd)
- Place black cells (represented as 1) in specific positions
- Fill remaining cells with white (represented as 0)
- Return the resulting grid as a 2D matrix
- The pattern follows a specific rule of expansion as n increases

# Visual Facts
1. Grid Properties:
   - Each grid is n×n where n is odd (1, 3, 5, 7, 9)
   - Each step adds 2 to n, creating a new outer ring

2. Cell States:
   - Cells are either black (filled) or white (empty)
   - For n=1: Single black cell
   - For n=3: Single black cell in center
   - For n=5: Five black cells
   - For n=7: Five black cells
   - For n=9: Nine black cells


# Visual Patterns
A cell at position [i][j] is black when either:
   1. It's at the center (i = j = n//2)
   2. It's 2*k units away from center vertically: j = n//2 & |i-n//2| = 2k, where k ≥ 1
   3. It's 2*k units away from center horizontally: i = n//2 & |j-n//2| = 2k, where k ≥ 1

This means black cells appear at:
- Center position
- Positions where lies in the same row or column as the center and its distance from the center is a multiple of 2
- All other cells are white.

This explains:
- n=1: Center only
- n=3: Center only
- n=5: Center + 4 cells at distance 2
- n=7: Same as n=5
- n=9: Center + 4 cells at distance 2 + 4 cells at distance 4


# Solution
from typing import List

def solution(n: int) -> List[List[int]]:
    """
    Given an n x n grid, determine the coordinates of a specific pattern that is formed on the grid. 
    
    Parameters:
        n (int): The dimensions of the grid (which is an n*n 2D matrix). n is always an odd number.
    
    Returns:
        List[List[int]]: A n*n 2D matrix where the coordinates of the black cells are marked with 1 and the rest are marked with 0.
    """
    grid = [[0 for _ in range(n)] for _ in range(n)]
    
    # Middle index
    mid = n // 2
    
    # Set the center
    grid[mid][mid] = 1
    
    # Set the diagonals
    for i in range(mid + 1):
        if (i-mid) % 2 == 1:
            continue
        grid[i][mid] = 1
        grid[mid][i] = 1
        grid[n - 1 - i][mid] = 1
        grid[mid][n - 1 - i] = 1
    
    return grid


# Test Script
def reference(n):
    grid = [[0 for _ in range(n)] for _ in range(n)]
    
    # Middle index
    mid = n // 2
    
    # Set the center
    grid[mid][mid] = 1
    
    # Set the diagonals
    for i in range(mid + 1):
        if (i-mid) % 2 == 1:
            continue
        grid[i][mid] = 1
        grid[mid][i] = 1
        grid[n - 1 - i][mid] = 1
        grid[mid][n - 1 - i] = 1
    
    return grid


def test():
    for n in range(3, 40, 4):
        try:
            output = solution(n)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`n`: {n}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message
            
        expected_output = reference(n)
        
        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`n`: {n}\n\n" 
                f"Actual Output (n x n matrix):\n"
                f"{'\n'.join(str(row) for row in output)}\n\n"
                f"Expected Output (n x n matrix):\n"
                f"{'\n'.join(str(row) for row in expected_output)}"
            )
            assert False, error_message

test()