#include <cstdio>
#include <string>
#include <map>

using namespace std;

int h, w;
char str[32][32];

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

void generate(int sy, int sx, map<string, int> &rec)
{
	char tmp[2] = {0, 0};
	
	for (int i = 0; i < 8; i++){
		tmp[0] = str[sy][sx];
		string s = tmp;
		int ny = sy, nx = sx;
		while (1){
			ny = (ny + h + dy[i]) % h; nx = (nx + w + dx[i]) % w;
			if (ny == sy && nx == sx) break;
			tmp[0] = str[ny][nx];
			s += tmp;
			if (rec.count(s) > 0) rec[s] += 1;
			else rec[s] = 1;
		}
	}
}

int main()
{
	while (scanf("%d %d", &h, &w) && h){
		for (int i = 0; i < h; i++) scanf("%s", str[i]);
		map<string, int> occ;
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				generate(i, j, occ);
			}
		}
		
		string ans = "";
		for (map<string, int>::iterator it = occ.begin(); it != occ.end(); it++){
			if (it->second > 1 && it->first.size() >= ans.size()){
				if (it->first.size() > ans.size()) ans = it->first;
				else if (it->first.size() == ans.size() && it->first < ans) ans = it->second;
			}
		}
		
		if (ans != "") printf("%s\n", ans.c_str());
		else printf("0\n");
	}
	return (0);
}