JAG-channel II

Time limit3sMemory limit256 MB

Summary
Find the lexicographically smallest member posting order consistent with the recorded top-to-bottom thread picks under move-to-front reordering.
Level

Medium7 of 10

Topics
Backtracking, Simulation, Bit manipulation
Solved
No attempts yet

Problem

JAG is a group of NN members who work on spreading competitive programming. The members talk every day on a board called JAG-channel. The board holds several threads, and the list is always sorted by the time of the latest post, newest first. As soon as someone posts in a thread, that thread moves to the top of the list.

One night each of the NN members created one thread. The members are labelled by the first NN uppercase letters, and each thread is written as the letter of the member who created it. The next morning each member posted once in each of KK different threads among the ones created that night. The members care about speed, so each of them scanned the list from top to bottom and posted in a thread the moment it caught their interest. The members posted in separate periods of time, so while one member was making their KK posts, no other member posted anything.

You know, for every member, the order of the threads they posted in, but you do not know the order the threads were in at the start. The list is reordered on every post, so some orders of the members are impossible: the posts of an earlier member can leave a later member unable to meet their threads in the recorded top-to-bottom order. Find the lexicographically smallest possible order of the members.

Input

The first line contains two integers NN and KK separated by one space (4≤N≤164 \le N \le 16, N−3≤K≤N−1N-3 \le K \le N-1).

Each of the next NN lines contains a string of exactly KK distinct uppercase letters. The jj-th character of the ii-th line is the thread in which the ii-th member made their jj-th post. A thread is written as the letter of the member who created it, so 'B' is the thread created by the second member, B.

At least one possible order of the members is guaranteed to exist.

Output

Print the lexicographically smallest possible order of the members as one line of NN uppercase letters. The ii-th character is the member who posted during the ii-th period.

Examples3

  1. Example 1

    Input
    7 4
    DEFG
    FEDA
    EFGB
    BGEA
    AGFD
    DABC
    CADE
    
    Expected output
    ABCDEFG
    
  2. Example 2

    Input
    4 3
    CDB
    DAC
    BAD
    ABC
    
    Expected output
    DCBA
    
  3. Example 3

    Input
    16 13
    NDHPFJIBLMCGK
    CMDJKPOLGIHNE
    MOLBIEJFPHADN
    KPNAOHBLMCGEI
    FCMLBHDOANJPK
    NHIGLOAPKJDMC
    KMLBIPHDEOANJ
    IEGCMLBOAPKJD
    JNAOEDHBLMCGF
    OEDHPFIBLMGKC
    GMLBIFPHDNAEO
    ENHGOPKJDMCAF
    JKPAOBLGEIHNF
    HPKFGJEIBLCOM
    LBINEJDAGFKPH
    FGMOCADJENIBL
    
    Expected output
    PONCAKJGIEDHMFBL