#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

#define MAX

vector<string> str;
map<string, int> mp;
int h, w, maximum, co;
string ans;


void check(int y, int x){
	vector<vector<int>> used(h,vector<int>(w,0));
	int count = 1;
	string res;
	// cout<<"y = "<<y<<" x = "<<x<<endl;
	for(int i = -1; i <= 1; i++){
		for(int j = -1; j <= 1; j++){
			string temp;
			// cout<<"i = "<<i<<" j = "<<j<<endl;
			for(int ny = y, nx = x; count != used[ny][nx]; nx = (j+w+nx)%w, ny = (i+h+ny)%h){
				used[ny][nx] = count;
				temp.push_back(str[ny][nx]);
				// cout<<temp<<endl;
				if(temp.size() <= 1) continue;
				
				if(++mp[temp] != 2) continue;
				if(ans.size() < temp.size()){
					ans = temp;
				} else if(ans.size() == temp.size() && ans > temp){
					ans = temp;
				}
			}
			
			count++;
		}
	}
	
	
	// return res;
}

signed main(){
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);
	
	while(true){
		mp.clear();
		ans.clear();
		
		cin>>h>>w;
		
		if(!h && !w) break;
		
		str.clear();
		str.resize(h);
		
		for(int i = 0; i < h; i++){
			cin>>str[i];
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				check(i,j);
			}
		}
		
		cout<<(ans.size()?ans:"0")<<endl;
	}
	
	
	return 0;
}
