#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define SZ(n)  (int)n.size()
#define dump(x) cerr<<#x<<" = "<<(x)<<endl;
#define debug(x)cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define MP make_pair
#define PB push_back
	


	int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,1,1,1,0,-1,-1,-1};
	string _or(string a,string b){
		if(SZ(a)>SZ(b))return a;
		if(SZ(a)<SZ(b))return b;
		return min(a,b);
	}
	char ta[50][50];
	int main(){
	int w,h;
	while(cin>>h>>w,(h||w)){
	map<string,int>ans;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>ta[i][j];
	string tmp;

	for(int ni=0;ni<h;ni++){
		for(int nj=0;nj<w;nj++){
			for(int p=0;p<8;p++){
				tmp.clear();
				int i=ni,j=nj;
				tmp.push_back(ta[ni][nj]);
					while(true){
					i+=dy[p],j+=dx[p];
					if(i<0)i=h-1;
					if(j<0)j=w-1;
					if(i>h-1)i=0;
					if(j>w-1)j=0;
					if(i==ni&&j==nj)break;
					tmp.push_back(ta[i][j]);

					if(SZ(tmp)>=2){
					ans[tmp]++;
					}
					}
			}
		}
	}
	string tans="";
	for(map<string,int>::iterator it=ans.begin();it!=ans.end();it++){
	if((*it).second>=2){tans=_or(tans,(*it).first);}
	}
	if(SZ(tans)<2)cout<<"0"<<endl;
	else cout<<tans<<endl;
	for(int i=0;i<50;i++)for(int j=0;j<50;j++)ta[i][j]=' ';
	}
	return 0;
} 