#include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int w, h;
string s[21];

string solve()
{
    vector<string> result;
    int cnt = 0, wcnt = 0, hcnt = 0;
    int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
    int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            // ???????????????s[i][j]
            for(int k = 0; k < 8; k++){
                string tmp = "";
                int sx = i, sy = j;
                while(!(sx == i && sy == j) || tmp.length() == 0){
                    tmp += s[sx][sy];
                    // cout << tmp << endl;
                    sx += dx[k];
                    sy += dy[k];
                    if(sx >= h) sx = 0;
                    if(sy >= w) sy = 0;
                    if(sx < 0) sx = h - 1;
                    if(sy < 0) sy = w - 1;
                }
                result.push_back(tmp);
            }
        }
    }
    sort(result.begin(), result.end());
    string ans = "";
    for(int i = 0; i < result.size() - 1; i++){
        // cout << result[i] << endl;
        if(result[i].length() < ans.length()) continue;
        int cnt = 0;
        string nowans = "";
        while(cnt < min(result[i].length(), result[i + 1].length())){
            if(result[i][cnt] == result[i + 1][cnt]){
                nowans += result[i][cnt];
                cnt++;
            } else {
                break;
            }
        }
        if(nowans.length() > ans.length()) ans = nowans;
    }
    if(ans.length() < 2) return "0";
    else return ans;
}

int main()
{
    while(1){
        cin >> h >> w;
        if(h == 0 && w == 0) break;
        for(int i = 0; i < h; i++){
            cin >> s[i];
        }
        cout << solve() << endl;
    }
    return 0;
}