#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

void init() {
    
}

const int dx[] = {0, 0, -1, -1, -1, 1, 1, 1};
const int dy[] = {1 ,-1, 1, 0, -1, 1, 0, -1};
const int MAX = 25;
char S[MAX][MAX];
map<string, int> cnt;

void solve(int h, int w) {
    for (int i = 0; i < h; i++) {
        scanf("%s", S[i]);
    }
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int d = 0; d < 8; d++) {
                string buf = "";
                set<pair<int, int>> se;
                //assert(buf == "");
                int ni = i, nj = j;
                while (true) {
                    if (se.count(make_pair(ni, nj))) break;
                    se.insert(make_pair(ni, nj));
                    buf += S[ni][nj];
                    if (buf.size() > 1) cnt[buf]++;
                    ni = (ni + dx[d] + h) % h;
                    nj = (nj + dy[d] + w) % w;
                }
                //DEBUG(buf)
                FORE(e, buf) assert(isupper(e));
            }
        }
    }
    string ans = "";
    for (auto e : cnt) {
        if (e.second > 1) {
            if (ans.length() <= e.first.length()) {
                if (ans.length() < e.first.length()) {
                    ans = e.first;
                } else if (ans > e.first) {
                    ans = e.first;
                }
            }
        }
    }
    if (ans == "") {
        ans = "0";
    }
    printf("%s\n", ans.c_str());
    cnt.clear();
}

signed main(void) {
    LL n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    init();
    while (cin >> n >> m, n) {
        solve(n, m);
    }
}

Measured memory_bytes: 38064