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++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <time.h>
#include <queue>
#include <stack>
#include <cctype>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <sstream>
#define Mod 1000000007
#define SMod 10007
#define INint 2147483647
#define pi acos(-1.0)
#define eps 1e-4
#define lll __int64
#define ll long long
using namespace std;
#define N 12
#define M 22

char ss[N][M];
int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,1,-1,-1,0,1};
string S1,S2,ans;
int n,m;

void go1(int x,int y,int prex,int prey,int k)
{
    if(x == prex && y == prey)
        return;
    S1 += ss[x][y];
    int nx = x+dx[k];
    int ny = y+dy[k];
    nx = (nx+n)%n;
    ny = (ny+m)%m;
    go1(nx,ny,prex,prey,k);
    return;
}

void go2(int x,int y,int prex,int prey,int k)
{
    if(x == prex && y == prey)
        return;
    S2 += ss[x][y];
    int nx = x+dx[k];
    int ny = y+dy[k];
    nx = (nx+n)%n;
    ny = (ny+m)%m;
    go2(nx,ny,prex,prey,k);
    return;
}

int main()
{
    int i,j,k,h,ka,kb,u,v;
    while(scanf("%d%d",&n,&m)!=EOF && n+m)
    {
        memset(ss,0,sizeof(ss));
        for(i=0;i<n;i++)
            scanf("%s",ss[i]);
        int tag = 0;
        int maxi = 0;
        ans = "";
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                for(k=0;k<n;k++)
                {
                    for(h=0;h<m;h++)
                    {
                        if(ss[i][j] == ss[k][h])
                        {
                            for(ka=0;ka<8;ka++)
                            {
                                for(kb=0;kb<8;kb++)
                                {
                                    if(i == k && j == h && ka == kb)
                                        continue;
                                    S1 = "";
                                    S1 += ss[i][j];
                                    int nx = i+dx[ka];
                                    int ny = j+dy[ka];
                                    nx = (nx+n)%n;
                                    ny = (ny+m)%m;
                                    go1(nx,ny,i,j,ka);
                                    //cout<<S1<<endl;
                                    S2 = "";
                                    S2 += ss[k][h];
                                    int kx = k+dx[kb];
                                    int ky = h+dy[kb];
                                    kx = (kx+n)%n;
                                    ky = (ky+m)%m;
                                    go2(kx,ky,k,h,kb);
                                    //cout<<S2<<endl;
                                    int cnt = 0;
                                    int len1 = S1.length();
                                    int len2 = S2.length();
                                    for(u=0;u<min(len1,len2);u++)
                                    {
                                        if(S1[u] != S2[u])
                                            break;
                                    }
                                    cnt = u;
                                    if(cnt > maxi)
                                    {
                                        ans = S1.substr(0,u);
                                        //cout<<"Sub: "<<S1.substr(0,u)<<endl;
                                        maxi = cnt;
                                    }
                                    else if(cnt == maxi)
                                    {
                                        if(ans > S1.substr(0,u))
                                            ans = S1.substr(0,u);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if(ans.length() <= 1)
            puts("0");
        else
            cout<<ans<<endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int h, w;
		cin >> h >> w;
		if (!h)break;
		vector<string> v(h);
		REP(i, h)cin >> v[i];
		set<string> st;
		string ans = "";
		REP(i, h) {
			REP(j, w) {
				REP(k, 8) {
					int ni = i, nj = j;
					string s;
					s += v[i][j];
					REP(l, INF) {
						if (!st.insert(s).second) {
							if (ans.size() < s.size())ans = s;
							else if (ans.size() == s.size() && ans > s)ans = s;
						}
						ni = (ni + dx[k] + h) % h, nj = (nj + dy[k] + w) % w;
						if (ni == i&&nj == j)break;
						s += v[ni][nj];

					}
				}
			}
		}
		if (ans.size() <= 1)cout << 0 << endl;
		else cout << ans << endl;
	}
	return 0;
}