#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int horieyui(const void* x, const void* y) {
	const char* a = (const char*)x, *b = (const char*)y;
	size_t aa = strlen(a), bb = strlen(b);
	if (aa != bb) return aa > bb ? -1 : 1;
	return strcmp(a, b);
}

int h, w;
char data[10][21];

char visited[10][21];

int candidateCount;
char candidates[10 * 20 * 8 * 10 * 20][10 * 20 + 1];

int main(void) {
	while (scanf("%d%d", &h, &w) == 2 && (h|w) != 0) {
		int i, j;
		int found;
		for (i = 0; i < h; i++) {
			if (scanf("%31s", data[i]) != 1) return 1;
		}
		candidateCount = 0;
		for (i = 0; i < h; i++) {
			for (j = 0; j < w; j++) {
				int dx, dy;
				for (dy = -1; dy <= 1; dy++) {
					for (dx = -1; dx <= 1; dx++) {
						if (dx != 0 || dy != 0) {
							int p = 0;
							int y = i, x = j;
							memset(visited, 0, sizeof(visited));
							while (!visited[y][x]) {
								visited[y][x] = 1;
								if(p > 0) strcpy(candidates[candidateCount], candidates[candidateCount - 1]);
								candidates[candidateCount][p] = data[y][x];
								candidates[candidateCount][p + 1] = '\0';
								candidateCount++;
								p++;
								y += dy;
								x += dx;
								if (y < 0) y += h;
								if (x < 0) x += w;
								if (y >= h) y -= h;
								if (x >= w) x -= w;
							}
						}
					}
				}
			}
		}
		qsort(candidates, candidateCount, sizeof(*candidates), horieyui);
		found = 0;
		for (i = 1; i < candidateCount; i++) {
			if (strlen(candidates[i]) < 2) break;
			if (strcmp(candidates[i - 1], candidates[i]) == 0) {
				puts(candidates[i]);
				found = 1;
				break;
			}
		}
		if (!found) puts("0");
	}
	return 0;
}

