#include <bits/stdc++.h>
using namespace std;

constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};

int solve(){
    int h, w;
    cin >> h >> w;
    if(h == 0) return 1;
    vector<string> s(h);
    for(int i=0;i<h;i++){
        cin >> s[i];
    }
    map<string,int> cnt;
    for(int k=0;k<8;k++){
        vector<vector<bool> > used(h, vector<bool>(w));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(used[i][j]) continue;
                int len = 0, ni = i, nj = j;
                string t;
                while(!used[ni][nj]){
                    t.push_back(s[ni][nj]);
                    used[ni][nj] = true;
                    ni = (ni+di8[k]+h)%h;
                    nj = (nj+dj8[k]+w)%w;
                }
                len = t.size();
                t += t;
                for(int l=0;l<len;l++){
                    for(int x=2;x<=len;x++){
                        cnt[t.substr(l,x)]++;
                    }
                }
            }
        }
    }
    string ans = "";
    for(auto p : cnt){
        string t = p.first;
        if(p.second >= 2){
            if(t.size() > ans.size()){
                ans = t;
            }else if(t.size() == ans.size()){
                ans = min(ans, t);
            }
        }
    }
    if(ans.empty()) ans = "0";
    cout << ans << endl;
    return 0;
}

int main(){
    while(solve()==0);
    return 0;
}

