#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <complex>
#include <set>
#include <map>
#include <cmath>
using namespace std;


int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};

int main(){
	int H,W;
	while(cin >> H >> W && H ){
		vector<string> s(H);
		for(int i = 0 ; i < H ; i++) cin >> s[i];
		
		
		map<string,int> answer;
		for(int i = 0 ; i < H ; i++){
			for(int j = 0 ; j < W ; j++){
				for(int d = 0 ; d < 8 ; d++){
					string wow;
					int done[20][20] = {};
					int x = j , y = i;
					while( done[y][x] == 0 ){
						wow += s[y][x];
						done[y][x] = true;
						answer[wow]++;
						x += dx[d];
						y += dy[d];
						x += W;
						y += H;
						x %= W;
						y %= H;
					}
				}
			}
		}
		int len = 0;
		for( map<string,int>::iterator it = answer.begin(); it != answer.end() ; it++){
			if( it->second < 2 || it->first.size() < 2 ) continue;
			len = max( len , (int)it->first.size() );
		}
		int flag = 0;
		for( map<string,int>::iterator it = answer.begin(); it != answer.end() ; it++){
			if( it->second < 2 || it->first.size() < 2 ) continue;
			if( len == it->first.size() ){
				cout << it->first << endl;
				flag = true;
				break;
			}
		}
		if(flag==0)cout << 0 << endl;
		
	}
}