import random,copy


def generateBoard():
    sizeOfBoard= random.randint(10,40)
    frequency = random.randint(10,90)
    board = [["." for i in range(sizeOfBoard)] for j in range(sizeOfBoard)]
    for i in range(len(board)):
        for j in range(len(board[i])):
            if random.randint(1,100) < frequency:
                board[i][j]="X"
    board = [list("#"*(len(board)))]+board+[list("#"*(len(board)))]
    board = [["#"]+x+["#"] for x in board]
    return board

def printM(matrix):
    for i in matrix:
        print(" ".join(i))

def dayOfLife(matrix):
    matrix2=copy.deepcopy(matrix)
    for r in range(1,len(matrix)-1):
        for c in range(1,len(matrix[r])-1):
            counter = -int(matrix[r][c]=="X")
            for rn in [r+1,r,r-1]:
                for cn in [c+1,c,c-1]:
                    counter += int(matrix[rn][cn]=="X")
            if matrix[r][c] == "X" and counter not in [3,4]:
                matrix2[r][c]="."
            elif matrix[r][c] == "." and counter == 3:
                matrix2[r][c]="X"
    return matrix2
                

board=generateBoard()
while input("-----------")!="stop":
    printM(board)
    board=dayOfLife(board)










            
