#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
int d1[8] = { 1,1,1,0,0,-1,-1,-1 };
int d2[8] = { 1,0,-1,1,-1,1,0,-1 };
int h, w;
int fh(int x) {
	return (x% h+h)%h;
}
int fw(int x) {
	return (x%w + w) % w;
}
int main() {
	while (cin >> h >> w, h) {
		char t[20][20];
		string s;
		rep(i, h) {
			cin >> s;
			rep(j, w) {
				t[i][j] = s[j];
			}
		}
		vector<string>v;
		rep(i, h) {
			rep(j, w) {
				rep(k, 8) {
				    string u;
				    u = t[i][j];
					int now = 1;
					while (!(fh(i + now*d1[k]) == i && fw(j + now*d2[k]) == j)) {
						u += t[fh(i + now * d1[k])][fw(j + now * d2[k])];
						v.push_back(u);
						now++;
					}
				}
			}
		}
		sort(v.begin(), v.end());
		int len = v.size();
		ui ma = 0; string out = "0";
		rep(i, len-1) {
			if (v[i] == v[i + 1] && ma < v[i].length()) {
				ma = v[i].length(); out = v[i];
			}
		}
		cout << out << endl;
	}
	return 0;
}
