#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE 

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

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

const int N = 20;
int visit[N][N];
int main(){
	int h, w;
	while (cin >> h >> w, h){
		vector<string> vs(h);
		rep(i, h) cin >> vs[i];

		map<string, int> m;
		rep(i, h) rep(j, w) rep(d, 8){
			MEMSET(visit, 0);
			string s;
			s += vs[i][j];
			visit[i][j] = 1;
			int nx = (j + dx[d] + w) % w, ny = (i + dy[d] + h) % h;
			while (!visit[ny][nx]){
				s += vs[ny][nx];
				++m[s];
				nx = (nx + dx[d] + w) % w, ny = (ny + dy[d] + h) % h;
			}
		}
		pair<int, string> ans = MP(0, "0");
		for (auto p : m){
			if (p.second > 1) ans = min(ans, MP(-(int)p.first.size(), p.first));
		}
		cout << ans.second << endl;
	}

	return 0;
}