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

int h, w;
void solve(int y, int x, vector<string> const& v, vector<string>& ret) {
    int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
        dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
    for(int i=0; i<8; ++i) {
        string s;
        vector<vector<bool>> used(h, vector<bool>(w, false));
        while(!used[y][x]) {
            s += v[y][x];
            used[y][x] = true;
            x = (x + dx[i] + w)%w;
            y = (y + dy[i] + h)%h;
            if(s.size() >= 2) {
                ret.push_back(s);
            }
        }
    }
}

int main() {
    while(cin >> h >> w, h) {
        vector<string> v(h);
        vector<string> ret;
        for(int i=0; i<h; ++i) {
            cin >> v[i];
        }
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                solve(i, j, v, ret);
            }
        }
        sort(ret.begin(), ret.end());
        string ret2;
        for(int i=0; i<ret.size()-1; ++i) {
            if(ret[i] == ret[i+1]) {
                if(ret2.size() < ret[i].size() || ret2.size() == ret[i].size() && ret2 > ret[i]) {
                    ret2 = ret[i];
                }
            }
        }
        if(ret2.size() == 0) {
            cout << 0 << endl;
        } else {
            cout << ret2 << endl;
        }
    }   
}
