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 <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int H, W;
char field[10][20];

int ma;
string mas;

set<string> S;

void add_string(int i, int n, int m, int x, int y) {
    REP(j, n) {
        int sx = x, sy = y;
        stringstream ss;
        ss << field[sy][sx];
        REP(k, m) {
            sx = (sx + dx[i] + W) % W;
            sy = (sy + dy[i] + H) % H;
            ss << field[sy][sx];
            string s = ss.str();
            if (!S.insert(s).second) {
                if (ma < (int)s.length()) {
                    ma = s.length();
                    mas = s;
                } if (ma == (int)s.length()) {
                    mas = min(mas, s);
                }
            }
        }
        x = (x + dx[i] + W) % W;
        y = (y + dy[i] + H) % H;
    }
}

int main() {
    while (cin >> H >> W, H|W) {
        REP(y, H) REP(x, W) cin >> field[y][x];
        S.clear();

        ma = 0;
        REP(i, 8) {
            if (i < 4) {
                if (i % 2 == 0) {
                    REP(y, H) add_string(i, W, W-1, 0, y);
                } else {
                    REP(x, W) add_string(i, H, H-1, x, 0);
                }
            } else {
                int n = (H == W ? H : H*W);
                REP(x, W) REP(y, H) {
                    add_string(i, 1, n-1, x, y);
                }
            }
        }
        if (ma == 0) {
            cout << 0 << endl;
        } else {
            cout << mas << endl;
        }
    }
}

C++: #include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

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

int w, h;
vector<string> s;

int main()
{
	while (cin >> h >> w, w)
	{
		s.resize(h);
		REP(i, h) cin >> s[i];
		map<string,int> used;
		REP(i, h)
		{
			REP(j, w)
			{
				REP(k, 8)
				{
					string t; t+=s[i][j];
					int nx = i; int ny = j;
					while (1)
					{
						nx += dx[k]; ny += dy[k];
						if (nx == h) nx = 0;
						else if (nx < 0) nx = h - 1;
						if (ny == w) ny = 0;
						else if (ny < 0) ny = w - 1;
						if (nx == i&&ny == j)  break;
						t += s[nx][ny];
						used[t]++;
					} 

				}
			}
		}
		string ans = "0";
		for (auto ite = used.begin(); ite != used.end(); ite++)
		{
			if ((*ite).second >= 2)
			{
				auto tmp = (*ite).first;
				if (ans.size() < tmp.size()) ans = tmp;
				else if (ans.size() == tmp.size()) ans = min(ans, tmp);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int lcm(int a,int b){	//a,b < 20だしいいよね
		if (a < b){
			swap(a,b);
		}
		int times = a * b;
		vector<int> num;
		int gcd,lcm;
		while(1){
			int n;
			n = a % b;
			if(n == 0){	break;}
			a = b;
			b = n;
		}
		gcd = b;
		lcm = times / gcd;
	return lcm;
}

int main(){
	int h,w;
	while(1){
		cin >> h >> w ;
		if(h == 0 && w == 0){	break;}
		char donut[10][20];
		for(int i=0;i<10;i++){
			for(int j=0;j<20;j++){
				donut[i][j] = 0;
			}
		}
		string code[1600];
		bool much[1600];
		for(int i = 0; i < 1600; i++){
			code[i] = "";
			much[i] = true;
		}	//初期化…

		int l = lcm(w,h);	//ななめの最大数
		int a = 0;	//strの数を数えます

		for(int i = 0; i < h; i++){
			for(int j =0; j < w; j++){
				cin >> donut[i][j];
			}
		}	//input

		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				for(int i = 0; i < h; i++){	//縦列
					code[a].append(1,donut[(y + i)%h][x]);
					code[a+1].append(1,donut[abs(h-(y + i)%h-1)][x]);
				}
				a += 2;
				for(int j = 0; j < w; j++){
					code[a].append(1,donut[y][(x + j)%w]);
					code[a+1].append(1,donut[y][abs(w-(x+j)%w-1)]);
				}
				a += 2;
				for(int k = 0; k < l; k++){	//ここが間違っている！！！！
					code[a].append(1,donut[(y + k)%h][(x + k)%w]);
					code[a+1].append(1,donut[(y + k)%h][(l + x - k)%w]);
					code[a+2].append(1,donut[(l + y - k)%h][(x + k)%w]);
					code[a+3].append(1,donut[(l + y - k)%h][(l + x - k)%w]);
				}
				a += 4;
			}
		}
		int b = a;
		string mag = "";
		//a:strの数-1

		sort(code,code+a);//nとn+1を比べます

		for(int i = 0; i < a-1; i++){
			if(code[i][0] != code[i+1][0]){
				much[i+1] = false;
			}
		}
		for(int i = 1; i < l; i++){	//2文字目から
			for(int j = a-1; j > 0; j--){	//逆順
				if(much[j] == true && code[j].length() > i+1 && code[j-1].length() > i+1){
					//jのi文字目とj-1のi文字目を比べるよ
					if(code[j][i] != code[j-1][i]){
						much[j] = false;
					}else{
						mag = code[j];
						mag.resize(i+1);
					}
				}
			}
		}
		if(mag == ""){
			cout << "0" << endl;
		}else{
			cout << mag << endl;
		}
	}
	return 0;
}