Go: package main

import "fmt"

var H, W int

type point struct {
	x, y int
}

func (p *point) add(q point) {
	p.x += q.x
	p.y += q.y
	if p.x < 0 {
		p.x += H
	}
	if p.y < 0 {
		p.y += W
	}
	p.x %= H
	p.y %= W
}

func isPalindrome(str string) bool {
	for i, j := 0, len(str)-1; i < j; i, j = i+1, j-1 {
		if str[i] != str[j] {
			return false
		}
	}
	return true
}

func magicSpel(arr []string) string {
	var ans string
	m := map[string]int{}
	for i := range arr {
		for j := range arr[i] {
			for _, d := range []point{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}} {
				cur := point{x: i, y: j}
				str := string(arr[cur.x][cur.y])
				cur.add(d)
				for i != cur.x || j != cur.y {
					str += string(arr[cur.x][cur.y])
					cur.add(d)
					if isPalindrome(str) {
						m[str] += 2
					} else {
						m[str]++
					}
				}
			}
		}
	}
	for s, v := range m {
		if 1 < v {
			if len(ans) < len(s) {
				ans = s
			} else if len(ans) == len(s) && s < ans {
				ans = s
			}
		}
	}

	if len(ans) == 0 {
		return "0"
	}

	return ans
}

func main() {
	for {
		fmt.Scan(&H, &W)
		if H == 0 && W == 0 {
			break
		}

		var arr []string

		for i := 0; i < H; i++ {
			var s string
			fmt.Scan(&s)
			arr = append(arr, s)
		}

		fmt.Println(magicSpel(arr))
	}
}

// 2 3
// ABC
// DEF

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;
long long int MOD = 1000000007;

int main(){
	
	while(true){
		int h, w;
		cin >> h >> w;
		if(h == 0){
			break;
		}
		string str[100];
		for(int i = 0; i < h; i++){
			cin >> str[i];
		}
		
		map<string, int> m;
		string ans = "";
		
		int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
		int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				for(int k = 0; k < 8; k++){
					bool used[h][w] = {};
					string S = "", D = " ";
					int y = i, x = j;
					while(true){
						if(used[y][x]){
							break;
						}
						used[y][x] = true;
						D[0] = str[y][x];
						S += D;
						y += dy[k] + h;
						x += dx[k] + w;
						y %= h;
						x %= w;
						if(m.find(S) == m.end()){
							m[S] = 1;
						}else{
							if(ans.size() < S.size()){
								ans = S;
							}else if(ans.size() == S.size() && ans > S){
								ans = S;
							}
						}
					}
				}
			}
		}
		if(ans.size() == 1){
			cout << 0 << endl;
		}else{
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;

int dx[] = {-1,0,1,1,1,0,-1,-1};
int dy[] = {1,1,1,0,-1,-1,-1,0};

using namespace std;
typedef pair<int ,int > P;
typedef long long ll;

int H,W;
string donut[11];
map<string, int> res;

void dfs(string str,int sy,int sx, int y,int x,int a) {
    int ny = (y+dy[a]+H) % H;
    int nx = (x+dx[a]+W) % W;
    res[str]++;
    if(sy==ny && sx==nx) return;

    dfs(str+donut[ny][nx],sy,sx,ny,nx,a);
}

string cmp(string s1,string s2) {
    if(s1.size() > s2.size()) return  s1;
    else if(s1.size() < s2.size()) return s2;
    else return s1 < s2 ? s1 : s2;
}

void solve() {
    res.clear();
    rep(i,11) donut[i]="";
    rep(i,H) cin>>donut[i];
    rep(y,H) rep(x,W) {
        rep(a,8) {
            string tmp;
            tmp = donut[y][x];
            dfs(tmp,y,x,y,x,a);
        }
    }
    string ans;
    for(const auto &it : res){
        if(it.sc >= 2) {
            ans = cmp(ans,it.fr);
        }
    }
    if(ans.size() <= 2) ans = "0";
    cout<<ans<<endl;
}

int main() {
    while(cin>>H>>W) {
        if(H+W==0) break;
        solve();
    }
    return 0;
}

C++: #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;
}