import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: m09kas4
 * Date: 18.11.2009
 * Time: 17:21:18
 * To change this template use File | Settings | File Templates.
 */
public class Points {

    public static void main( String[] args ) throws Exception {
            Scanner in = new Scanner( new File( "sorting.in") );
            PrintWriter out = new PrintWriter( new File( "sorting.out" ) );
            int n = in.nextInt();
            int m = in.nextInt();
            int[] k = new int[m];
            String str = "";
            ArrayList<String> aL = new ArrayList<String>();
            for (int i=0; i<m; i++) {
                aL.add( in.next() );
            }
            for (int i=0; i<m; i++)
                for (int j = 0; j < n; j++)
                    for (int l = j + 1; l < n; l++)
                        if (aL.get(i).charAt(j)>aL.get(i).charAt(l))
                            k[i]++;
        for (int i=0; i<m; i++)
            for (int j=0; j<m-1; j++)
                if (k[j]>k[j+1]) {
                     int o=k[j];
                    k[j]=k[j+1];
                    k[j+1]=o;
                    str = aL.get(j);
                    aL.set(j, aL.get(j+1));
                    aL.set(j+1, str);
                }
        for(int i = 0; i < m; i++)
        out.println( aL.get(i) );
        in.close();
        out.close();
    }
}
