#include <iostream>
#include <string>
#include <stdio.h>
#include <set>

using namespace std;

#define dirmax 8
int l[2];
int dir[8][2] = { {0,1}, {1,0}, {-1,0}, {0,-1}, {1,1}, {-1,1}, {1,-1}, {-1,-1} };
string ans;

void search( set<string> *v, int pos[2] , char ch[10][20]){

	for(int i = 0; i < dirmax; i++) {
		string str;
		int cpos[] = { pos[0], pos[1] };

		str += ch[pos[0]][pos[1]];

		while( 1 ) {
			for(int j = 0; j < 2; j++) {
				cpos[j] += dir[i][j];
				if( cpos[j] < 0 ) cpos[j] = l[j] - 1;
				if( cpos[j] >= l[j] ) cpos[j] = 0;
			}
			if( cpos[0] == pos[0] && cpos[1] == pos[1] ) break;
			str += ch[cpos[0]][cpos[1]];
			
			if( v->find(str) != v->end() )
				if( ( str.length() > ans.length() ) || ( str.length() == ans.length() && str < ans ) )
					ans = str;
			v->insert(str);
		}
	}
}

int main(void){
	while(1){
		cin >> l[0] >> l[1];
		if( l[0] == 0 && l[1] == 0 ) break;
		char donut[10][20];
		ans = "";

		set< string > strs;

		//入力を取得する
		for(int i = 0; i < l[0]; i++) {
			string str;
			cin >> str;
			for(int j = 0; j < l[1]; j++){
				donut[i][j] = str[j];
			}
		}

		for(int i = 0; i < l[0]; i++) {
			for(int j = 0; j < l[1]; j++) {
				int pos[2];
				pos[0] = i;
				pos[1] = j;
				search( &strs, pos, donut );
			}
		}

		if(ans == "") 
			cout << 0 << endl;
		else 
			cout << ans << endl;

	}
	return 0;
}