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 "cstring"
#include "cstdio"
using namespace std;
const int N = 320010;
const int CH = 26;
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {1, -1, 0, 1, -1, 0, 1, -1};
char s[30][30];
int trie[N][CH], word[N];
int sz = 0;
int n, m, ans, ax, ay, ap, al;
void gao(int &x, int &y)
{
	if(x < 0) x += n;
	if(y < 0) y += m;
	if(x >= n) x -= n;
	if(y >= m) y -= m;
}
bool judge(int x, int y, int p, int len)
{
	int sx1 = ax, sy1 = ay, sp1 = p;
	int sx2 = x, sy2 = y, sp2 = p;
	for(int i = 0; i < len; ++ i){
		if(s[sx1][sy1] > s[sx2][sy2]){
			return true;
		}else if(s[sx1][sy1] < s[sx2][sy2]){
			return false;
		}
		sx1 += dx[sp1];sy1 += dy[sp1];
		sx2 += dx[sp2];sy2 += dy[sp2];
		gao(sx1, sy1);
		gao(sx2, sy2);
	}
	return false;
}
void insert(int sx, int sy, int p)
{
	int r = 0;	
	int x = sx, y = sy;
	int length = 0;
	while(1){
		length ++;
		int idx = s[x][y] - 'A';
		if(!trie[r][idx]){
			trie[r][idx] = ++ sz;
			for(int j = 0; j < CH; ++ j){
				trie[sz][j] = 0;
			}
			word[sz] = 0;
		}		
		r = trie[r][idx];
		if(word[r] > 0){
			if(ans < length){
				ans = length;
				ax = sx, ay = sy, ap = p, al = length;
			}else if(ans == length){
				if(judge(sx, sy, p, length)){
					ans = length;
					ax = sx, ay = sy, ap = p, al = length;
				}			
			}
		}
		word[r] ++;
		x = x + dx[p];
		y = y + dy[p];
		gao(x, y);
		if(x == sx && y == sy) break;
	}
}	
void Init()
{
	sz = 0;
	memset(trie[0], 0, sizeof(trie[0]));
}
void print()
{
	int x = ax, y = ay, p = ap;
	for(int i = 0 ; i < al; ++ i){
		printf("%c",s[x][y]);
		x = x + dx[p];
		y = y + dy[p];
		gao(x, y);
	}
	printf("\n");
}
int main(void)
{
	while(scanf("%d%d",&n,&m) && (n || m)){
		for(int i = 0; i < n ; ++ i){
			scanf("%s", s[i]);
		}
		Init();
		ans = -1;
		for(int i = 0; i < n; ++ i){
			for(int j = 0; j < m; ++ j){
				for(int k = 0; k < 8 ; ++ k){
					insert(i, j, k);
				}
			}
		}
		if(ans < 2){
			printf("0\n");
		}else{
			print();
		}
	}
	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;
    if(sy==ny && sx==nx) return;

    res[str]++;
    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 <iostream>
#include <string>
#include <stdio.h>
#include <set>

using namespace std;

#define dirmax 8

int l[2];
int dir[8][2] = { {0,1}, {1,0}, {-1,0}, {0,-1}, {1,1}, {-1,1}, {1,-1}, {-1,-1} };
string ans;

void search( set<string> *v, int pos[2] , char ch[10][20]){

	for(int i = 0; i < dirmax; i++) {
		string str;
		int cpos[] = { pos[0], pos[1] };

		str += ch[pos[0]][pos[1]];

		while( 1 ) {
			for(int j = 0; j < 2; j++) {
				cpos[j] += dir[i][j];
				if( cpos[j] < 0 ) cpos[j] = l[j] - 1;
				if( cpos[j] >= l[j] ) cpos[j] = 0;
			}
			if( cpos[0] == pos[0] && cpos[1] == pos[1] ) break;

			str += ch[cpos[0]][cpos[1]];
			
			if( v->find(str) != v->end() ) {
 				if( ( str.length() > ans.length() ) || ( str.length() == ans.length() && str < ans ) ) {
					ans = str;
				}
			} else 
				v->insert(str);
		}
	}
}

int main(void){
	while(1){
		cin >> l[0] >> l[1];
		if( l[0] == 0 && l[1] == 0 ) break;

		char donut[10][20];
		int pos[2];
		ans = "";

		set< string > strs;

		for(int i = 0; i < l[0]; i++)
			cin >> donut[i];
		
		for(pos[0] = 0; pos[0] < l[0]; pos[0]++)
			for(pos[1] = 0; pos[1] < l[1]; pos[1]++)
				search( &strs, pos, donut );

		if(ans == "") 
			cout << 0 << endl;
		else 
			cout << ans << endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#pragma warning(disable:4996)
using namespace std;

long long int gcd(long long int l, long long int r) {
	if (l > r)return gcd(r, l);
	else {
		if (r%l) {
			return gcd(l, r%l);

		}
		else {
			return l;
		}
	}
}
int dx[8] = { -1,-1,-1, 0, 1, 1, 1, 0 };
int dy[8] = { -1, 0, 1, 1, 1, 0,-1,-1 };

class Compare {
public:
	//keiroが昇順に並ぶ
	bool operator()(const string&l, const string&r) {
		return l.size() == r.size() ? l<r : l.size() < r.size();

	}
};


int main() {
	while (1) {
		int H, W; cin >> H >> W;
		if (!H)break;
		vector<string>field;
		for (int i = 0; i < H; ++i) {
			string st; cin >> st;
			field.push_back(st);
		}
		int ytime = H;
		int xtime = W;
		int yxtime = H*W/ gcd(H,W);
		map<string, int>mp;
		for (int y = 0; y < H; ++y) {
			for (int x = 0; x < W; ++x) {
				for (int i = 0; i < 8; ++i) {
					int ny(y);
					int nx(x);
					int atime;
					if (i % 2==0) {
						atime = yxtime;
					}
					else if (i == 1 || i == 5) {
						atime = xtime;
					}
					else {
						atime = ytime;
					}
					string nst;
					for (int k = 0; k < atime; ++k) {
						nst += field[ny][nx];
						if (k) {
							mp[nst]++;
						}
						ny = (ny + H + dy[i]) % H;
						nx = (nx + W + dx[i]) % W;
					}
				}
			}
		}
		string ans = "0";
		int num = 0;
		for (auto it = mp.begin(); it != mp.end(); ++it) {
			if (it->second >= 2) {
				if (num < it->first.size()) {
					ans = it->first;
					num = it->first.size();
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}