#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;


int main() {
    int H, W;
    while (cin >> H >> W, H) {
        string field[10];
        for (int i = 0; i < H; i++) {
            cin >> field[i];
        }

        vector<string> cand;
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= +1; dy++) {
                        if (dx == 0 && dy == 0) continue;
                        bool visited[15][25] = { 0 };
                        int nx = x, ny = y;
                        string spell;
                        while (!visited[ny][nx]) {
                            visited[ny][nx] = true;
                            spell += field[ny][nx];
                            cand.push_back(spell);
                            nx = (nx + dx + W) % W;
                            ny = (ny + dy + H) % H;
                        }
                    }
                }
            }
        }
        
        string ans = "";
        sort(cand.begin(), cand.end());
        for (int i = 1; i < (int)cand.size(); ++i) {
            if (cand[i].size() == 1) continue;
            if (cand[i] != cand[i - 1]) continue;
            if (ans.size() < cand[i].size() || ans > cand[i]) {
                ans = cand[i];
            }
        }
        if (ans == "") ans = "0";
        cout << ans << endl;
    }
}