// Arup Guha
// 7/13/2026
// Solution to 1994 UCF HSPT Problem: Snail

import java.util.*;

public class snail {

	// In the order they want us to snail.
	final public static int[] DR = {0, 1,  0, -1};
	final public static int[] DC = {1, 0, -1,  0};
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		
		// Process cases.
		while (stdin.hasNext()) {
		
			// Get size.
			int r = stdin.nextInt();
			int c = stdin.nextInt();
			char[][] g = new char[r][c];
			
			// Read grid.
			for (int i=0; i<r; i++)
				for (int j=0; j<c; j++)
					g[i][j] = stdin.next().charAt(0);
			
			// What I've "printed".
			boolean[][] used = new boolean[r][c];
			
			// Store answer here.
			char[] res = new char[r*c];
			
			// Where I am now, and index int res.
			int cR = 0, cC = 0, idx = 0, dir=0;
			
			// Will do each character.
			for (int i=0; i<r*c; i++) {
			
				res[idx++] = g[cR][cC];
				used[cR][cC] = true;
				
				// Where we "would go"
				int nR = cR + DR[dir];
				int nC = cC + DC[dir];
				
				// We must change direction!
				if (!inbounds(nR,nC,r,c) || used[nR][nC]) {
					
					// Go to the next direction.
					dir = (dir+1)%DR.length;
					
					// This is correct.
					nR = cR + DR[dir];
					nC = cC + DC[dir];
				}
				
				// Update where we are.
				cR = nR;
				cC = nC;
			}
			
			// Ta da!
			System.out.println(new String(res));
			System.out.println();
		}
	}
	
	// Returns true iff (myR, myC) is within an r by c grid.
	public static boolean inbounds(int myR, int myC, int r, int c) {
			return myR >= 0 && myR < r && myC >= 0 && myC < c;
	}
}