Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.

You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below: 
 -  illegal — if the given board layout can't appear during a valid game; -  the first player won — if in the given board layout the first player has just won; -  the second player won — if in the given board layout the second player has just won; -  draw — if the given board layout has just let to a draw.
The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).
def check_row_column(field, c, i):
    win_row = True
    win_column = True
    for j in range(3):
        if field[i][j] != c:
            win_row = False
        if field[j][i] != c:
            win_column = False

    # if win_row and win_column:
    #     return None
    return win_row or win_column


def check_rows_columns(field, c):
    # win = []
    for i in range(3):
        if check_row_column(field, c, i):
            return True
    return False
    #     row_column = check_row_column(field, c, i)
    #     if row_column is None:
    #         return None
    #     else:
    #         win.append(row_column)
    # if sum(win) > 1:
    #     return None
    # return any(win)


def check_diags(field, c):
    win_diag_1 = True
    win_diag_2 = True
    for i in range(3):
        if field[i][i] != c:
            win_diag_1 = False
        if field[i][2 - i] != c:
            win_diag_2 = False

    # if win_diag_1 and win_diag_2:
    #     return None
    return win_diag_1 or win_diag_2


def check_win(field, c):
    # rows_columns = check_rows_columns(field, c)
    # diags = check_diags(field, c)
    # if rows_columns is None or diags is None:
    #     return None
    return check_rows_columns(field, c) or check_diags(field, c)


def main():
    field = []
    cross = 0
    zero = 0
    draw = True

    for _ in range(3):
        row = input()
        arr = []
        for c in row:
            arr.append(c)
            if c == "X":
                cross += 1
            elif c == "0":
                zero += 1
            elif c == ".":
                draw = False
        field.append(arr)

    if draw and cross != zero + 1 or not draw and cross != zero + 1 and cross != zero:
        print("illegal")
        return

    win_cross = check_win(field, "X")
    win_zero = check_win(field, "0")

    # if (win_zero is None or win_cross is None or
    if (win_cross and win_zero or
                win_cross and cross != zero + 1 or
                win_zero and win_cross != win_zero):
        print("illegal")
        return

    if win_cross:
        print("the first player won")
    elif win_zero:
        print("the second player won")
    elif draw:
        print("draw")
    elif cross == zero:
        print("first")
    else:
        print("second")


if __name__ == '__main__':
    main()
