import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.lang.String;

/**
 * Created by IntelliJ IDEA.
 * User: m09fpv
 * Date: 18.11.2009
 * Time: 17:10:45
 * To change this template use File | Settings | File Templates.
 */
public class sorting {
  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();
    String s,st; int i,j,k,t;
    int[] a=new int[m]; String[] q=new String[m];
    for(i=0;i<m;i++){ s=in.next(); a[i]=0; q[i]=s;
     for(j=1;j<n;j++){
      for(k=0;k<j;k++){
       if(s.charAt(k)>s.charAt(j)){  a[i]++; }
                     }
                      }
                      }
    for(i=1;i<m;i++){
     for(j=0;j<i;j++){
      if(a[j]>a[i]){ t=a[i]; a[i]=a[j]; a[j]=t;
                     st=q[i]; q[i]=q[j]; q[j]=st; }
                     }
                     }
    for(i=0;i<m;i++){ out.println(q[i]); }
    in.close();
    out.close();
}
}