program main
    implicit none
    integer n, l
    character(:), allocatable, dimension(:) :: s
    read *, n, l
    allocate(character(l) :: s(n))
    read *, s
    call bubblesort(s, smallerDict)
    print *, s
!    write (*, fmt='(a)', advance='no') s

contains
    recursive logical function smallerDict(str1, str2) result(ret)
        character(*), intent(in) :: str1, str2

        if ( len_trim(str2) == 0 ) then
            ret = .false.
        endif

        if ( len_trim(str1) == 0 ) then
            ret = .true.
        endif

        if ( iachar(str1(1:1)) < iachar(str2(1:1)) ) then
            ret = .true.
        else if ( iachar(str1(1:1)) > iachar(str2(1:1)) ) then
            ret = .false.
        else
            ret = smallerDict(str1(2:), str2(2:))
        endif
    end function

    subroutine bubblesort(array, compareFunction)
        character(*), dimension(:), intent(inout) :: array

        interface
            logical function compareFunction(str1, str2)
                implicit none
                character(*), intent(in) :: str1, str2
            end function
        end interface

        integer i
        character(len(array(1))) temp
        logical update

        do
            update = .false.
            do i=1, size(array) - 1
                if ( .not. compareFunction(array(i), array(i + 1)) ) then
                    temp = array(i)
                    array(i) = array(i + 1)
                    array(i + 1) = temp
                    update = .true.
                endif
            end do
            if ( .not. update ) then
                exit
            endif
        end do
    end subroutine
end program main
