#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using uInt = long long unsigned int;
using Double = long double;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
class Solve {
public:
    Int N, M;
    vector<string> A;
    map<string, Int> mp;

    Int mod_add(Int a, Int b, Int m) {
        Int ret = a + b;
        ret %= m;
        if (ret < 0) ret += m;
        return ret;
    }

    void rekkyo(Int sh, Int sw, Int dh, Int dw) {
        Int h = sh, w = sw;
        vector<vector<bool>> used(N, vector<bool>(M, false));
        string s;

        if (dh == 0 && dw == 0) {
            s += A[h][w];
            mp[s]++;
            return;
        }

        while (true) {
            if (used[h][w]) {
                break;
            }
            used[h][w] = true;
            s += A[h][w];
            if (s.size() > 1)
                mp[s]++;

            h = mod_add(h, dh, N);
            w = mod_add(w, dw, M);
        }
    }

    bool solve() {
        cin >> N >> M;
        if (N == 0) return false;
        A.resize(N);
        rep(i, 0, N) {
            cin >> A[i];
        }

        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                for (int dh = -1; dh <= 1; ++dh) {
                    for (int dw = -1; dw <= 1; ++dw) {
                        rekkyo(i, j, dh, dw);
                    }
                }
            }
        }

        string ans;
        for (auto e : mp) {
            if (e.second >= 2 && e.first.size() >= 2) {
                string s = e.first;
                if (s.size() > ans.size()) {
                    ans = s;
                } else {
                    chmin(ans, s);
                }
            }
//            cerr << e.first << endl;
        }
        if (ans == "") {
            cout << 0 << endl;
        } else {
            cout << ans << endl;
        }


        return true;
    }

};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    while (Solve().solve());


    return 0;
}



Measured memory_bytes: 38016