#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

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

int n, m, ml, cnt;
char donut[maxn][maxn];
string ans[2000];
map<string, int> mp;

int main()
{
#ifdef LOCAL
	freopen("B.in", "r", stdin);
#endif
	while(scanf("%d%d", &n, &m) != EOF) {
		if(n == 0 && m == 0)	break;
		ml = cnt = 0;
		mp.clear();
		for(int i = 0; i < n; i++)
			scanf("%s", donut[i]);
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) 
				for(int k = 0; k < 8; k++) {
					string tmp = "";
					int nx = i, ny = j;
					do{
						tmp += donut[nx][ny];
						if(tmp.size() >= 2 && tmp.size() >= ml) {
							if(!mp[tmp])	mp.insert(pair <string, int> (tmp, 1));	
							else if(tmp.size() == ml && mp[tmp] == 1) 
								ans[cnt++] = tmp;
							else if(tmp.size() > ml) {
								ml = tmp.size();
								cnt = 0;
								ans[cnt++] = tmp;
							}
						}
						mp[tmp]++;						
						nx = (nx+dx[k]+n)%n;
						ny = (ny+dy[k]+m)%m;
					}while(nx != i || ny != j);	
//					cout << tmp << endl;	
					/*
					if(!mp[tmp])	mp.insert(pair <string, int> (tmp, 1));	
					else {
						if(tmp.size() == ml && mp[tmp] == 1) {
							ans[cnt++] = tmp;
						}
						else if(tmp.size() > ml) {
							ml = tmp.size();
							cnt = 0;
							ans[cnt++] = tmp;
						}
						mp[tmp]++;
					}
					*/
				}
		sort(ans, ans+cnt);
		if(cnt)	printf("%s\n", ans[0].c_str());
		else	printf("0\n");
	}
	return 0;
}