import java.util.*;
import java.io.*;
import java.math.*;

import javax.xml.crypto.Data;

public class Main {
	static PrintWriter out = new PrintWriter(System.out);
	static FastScanner sc = new FastScanner();
	static Scanner stdIn = new Scanner(System.in);
	static int[] vx = new int[] {0,1,0,-1,1,1,-1,-1};
	static int[] vy = new int[] {1,0,-1,0,1,-1,1,-1};
	public static void main(String[] args) {
		
		while(true) {
			int h = sc.nextInt();
			int w = sc.nextInt();
			if(h == 0 && w == 0) break;
			int maxLen = 2;
			HashMap<String,Integer> list = new HashMap<String,Integer>();
			ArrayList<String> cd = new ArrayList<String>();
			
			char[][] map = new char[h][];
			for(int i = 0; i < h; i++) {
				map[i] = sc.next().toCharArray();
			}
			
			
			for(int i = 0; i < h; i++) {
				for(int j = 0; j < w; j++) {
					
					for(int k = 0; k < 8; k++) {
						StringBuilder sb = new StringBuilder();
						int nx = j;
						int ny = i;
						while(true) {
							sb.append(map[ny][nx]);
							nx += vx[k];
							ny += vy[k];
							if(nx < 0) nx = w-1;
							if(nx >= w) nx = 0;
							if(ny < 0) ny = h-1;
							if(ny >= h) ny = 0;
							if(nx == j && ny == i) break;
							
						}
						

						for(int l = maxLen; l <= sb.length(); l++) {
							String tmp = sb.substring(0, l).toString();
							
							if(list.containsKey(tmp)) {
								if(list.get(tmp) == 1) {
									list.put(tmp, -1);
									cd.add(tmp);
									maxLen = Math.max(maxLen, tmp.length());
								}
							}
							else {
								list.put(tmp, 1);
							}
						}
						
					}
				}
			}
			Collections.sort(cd,new MyComp());
			String ans = "0";
			if(!cd.isEmpty()) {
				ans = cd.get(0);
			}
			out.println(ans);
			
			
			
			
		}
		out.flush();
	}
}

class MyComp implements Comparator<String> {

	@Override
	public int compare(String o1, String o2) {
		if(o1.length() < o2.length()) {
			return 1;
		}
		else if(o1.length() > o2.length()) {
			return -1;
		}
		return o1.compareTo(o2);
	}
	
}

//-----------------Template------------------//
class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		}else{
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
	public boolean hasNext() { skipUnprintable(); return hasNextByte();}
	public String next() {
		if (!hasNext()) throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext()) throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while(true){
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			}else if(b == -1 || !isPrintableChar(b)){
				return minus ? -n : n;
			}else{
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	 
	public int nextInt() {
		 if (!hasNext()) throw new NoSuchElementException();
		 int n = 0;
		 boolean minus = false;
		 int b = readByte();
		 if (b == '-') {
			 minus = true;
			 b = readByte();
		 }
		 if (b < '0' || '9' < b) {
			 throw new NumberFormatException();
		 }
		 while(true){
			 if ('0' <= b && b <= '9') {
				 n *= 10;
				 n += b - '0';
			 }else if(b == -1 || !isPrintableChar(b)){
				 return minus ? -n : n;
			 }else{
				 throw new NumberFormatException();
			 }
			 b = readByte();
		 }
	}
	 
	public double nextDouble() {
		return Double.parseDouble(next());
	}
	 
 
}

Measured memory_bytes: 127176