#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

struct Node {
	int x, y, d;
	Node(int a, int b, int c) {
		x = a, y = b, d = c;
	}
	bool operator<(const Node&n)const {
		return make_pair(x, y) < make_pair(n.x, n.y);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> H >> W, H) {
		vector<string>s(H);
		for (auto &i : s)cin >> i;
		map<string, set<Node>>st;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				for (int k = 0; k < 8; k++) {
					string box;
					vector<vector<int>>visited(H, vector<int>(W));
					int ny = i, nx = j;
					while (!visited[ny][nx]) {
						visited[ny][nx] = 1;
						box.push_back(s[ny][nx]);
						ny += dy[k];
						nx += dx[k];
						if (ny < 0)ny += H;
						if (ny >= H)ny -= H;
						if (nx < 0)nx += W;
						if (nx >= W)nx -= W;
						st[box].insert(Node(i, j, k));
					}
				}
			}
		}
		string ans = "";
		for (auto i : st) {
			if (i.second.size() == 1)continue;
			if (ans.size() < i.first.size())ans = i.first;
		}
		if (ans.size() <= 1) {
			cout << 0 << endl;
		}
		else cout << ans << endl;
	}
}
