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

namespace MaxNumber
{
    public class MaxNumberMiXa
    {
        public static int N, M;
        public static string[] a, initial;
        public static string ans = "0";

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

        public static void MakeA()
        {
            a = (string[])initial.Clone();
            Array.Sort(a);
            a = a.Reverse().ToArray();
        }

        static void CheckString(string s)
        {
            for (int i = 0; i < M; i++)
            {
                if (s[i] >= ans[0])
                {
                    var st = s.Substring(i) + s.Substring(0, i);
                    if (st.CompareTo(ans) > 0)
                        ans = st;
                }
            }
        }

        static void CheckLines()
        {
            for (int i = 0; i < N; i++)
            {
                var s = new StringBuilder();
                s.Append(a[i]);
                for (int j = 0; j < N; j++)
                {
                    if (i != j)
                    {
                        s.Append(a[j]);
                    }
                }
                CheckString(s.ToString());
            }
        }

        public static void TransA()
        {
            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(initial[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();
            }
            Array.Sort(a);
            a = a.Reverse().ToArray();
        }

        public static void Solve()
        {
            CheckLines();
            TransA();
            CheckLines();
        }

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