N = 8

def print_board(board):

    for row in board:
        print(" ".join(row))

def is_safe(board, row, col):

    for i in range(col):
        if board[row][i] == 'Q':
            return False

    i = row
    j = col

    while i >= 0 and j >= 0:
        if board[i][j] == 'Q':
            return False
        i -= 1
        j -= 1

    i = row
    j = col

    while i < N and j >= 0:
        if board[i][j] == 'Q':
            return False
        i += 1
        j -= 1

    return True

def solve(board, col):

    if col >= N:
        return True

    for i in range(N):

        if is_safe(board, i, col):

            board[i][col] = 'Q'

            if solve(board, col + 1):
                return True

            board[i][col] = '.'

    return False

board = [['.' for _ in range(N)] for _ in range(N)]

solve(board, 0)

print("8 Queens Solution:\n")

print_board(board)