using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MaxNumber
{
    class MaxNumberSlowMiXa
    {
        static int N, M;
        static string[] a;
        static string ans = "0";
        static char firstChar;

        static void Init()
        {
            var line = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            N = line[0];
            M = line[1];
            a = new string[N];
            for (int i = 0; i < N; i++)
            {
                a[i] = Console.ReadLine();
                for (int j = 0; j < M; j++)
                {
                    if (a[i][j] > firstChar)
                    {
                        firstChar = a[i][j];
                    }
                }
            }
        }

        static void CheckPermutation(int[] order)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < N; i++)
            {
                sb.Append(a[order[i]]);
            }
            var s = sb.ToString();
            for (int i = 0; i < N*M; i++)
            {
                if (s[i] >= firstChar)
                {
                    var st = s.Substring(i) + s.Substring(0, i);
                    if (st.CompareTo(ans) > 0)
                        ans = st;
                }
            }
        }

        static void FindBestPermutation(int[] order, int startIndex)
        {
            if (startIndex == N)
            {
                CheckPermutation(order);
                return;
            }
            for (int i = 0; i < N; i++)
            {
                if (Array.IndexOf(order, i, 0, startIndex) == -1)
                {
                    order[startIndex] = i;
                    FindBestPermutation(order, startIndex + 1);
                }
            }
        }

        static void Solve()
        {
            int[] order = new int[N];
            order[0] = 0;
            FindBestPermutation(order, 1);
            var b = new StringBuilder[M];
            for (int j = 0; j < M; j++)
            {
                b[j] = new StringBuilder();
            }
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    b[j].Append(a[i][j]);
                }
            }
            var o = N;
            N = M;
            M = o;
            a = new string[N];
            for (int i = 0; i < N; i++)
            {
                a[i] = b[i].ToString();
            }
            order = new int[N];
            order[0] = 0;
            FindBestPermutation(order, 1);
        }

        static void Main(string[] args)
        {
            Init();
            Solve();
            Console.WriteLine(ans);
        }
    }
}
