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 <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;

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

    for(;;){
        int h, w;
        cin >> h >> w;
        if(h == 0)
        return 0;

        vector<string> grid(h);
        for(int i=0; i<h; ++i)
        cin >> grid[i];

        set<string> ss;
        string ret;
        int maxLen = -1;
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int d=0; d<8; ++d){
                    int y1 = y;
                    int x1 = x;
                    string s;
                    do{
                        s += grid[y1][x1];
                        y1 += dy[d] + h;
                        x1 += dx[d] + w;
                        y1 %= h;
                        x1 %= w;
                    }while(y1 != y || x1 != x);

                    int n = s.size();
                    while(n >= max(maxLen, 2)){
                        string t = s.substr(0, n);
                        if(ss.find(t) == ss.end()){
                            ss.insert(t);
                        }else{
                            if(n > maxLen || (n == maxLen && t < ret)){
                                maxLen = n;
                                ret = t;
                            }
                            break;
                        }
                        -- n;
                    }
                }
            }
        }

        if(maxLen == -1)
        cout << 0 << endl;
        else
        cout << ret << endl;
    }
}

C++: #include "bits/stdc++.h"
using namespace std;
#define ll long long
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1316

int h, w;

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

set<string> Set;
int Maxlen = 0;
string ans;
vector<vector<char>> donut;

void solve(int i, int j) {
	for (int k = 0; k < 8;k++) {
		vector<vector<int>> usage(h, vector<int>(w, 0));
		string ret = "";
		int y = i, x = j;
		while (usage[y][x] == 0) {
			ret += donut[y][x];

			if (Set.find(ret) != Set.end()) {
				if (ret.length() > Maxlen) {
					Maxlen = ret.length();
					ans = ret;
				}
				else if (ret.length() == Maxlen && ret < ans) {
					ans = ret;
				}
			}
			else {
				Set.insert(ret);
			}

			usage[y][x] = 1;
			y = (y + dy[k] + h) % h;
			x = (x + dx[k] + w) % w;


		}
	}
}

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	while (cin >> h >> w) {
		if (h == 0 && w == 0)break;
		Maxlen = 0;
		ans = "";
		donut.resize(h, vector<char>(w));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> donut[i][j];
			}
		}

		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				solve(i, j);
			}
		}
		cout << ans << endl;
	}

}

C++: #include <bits/stdc++.h>
using namespace std;
const int MAX_H=11,MAX_W=21;
const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};

int h,w,x,y; string now,ans;
string S[MAX_H];
int visited[MAX_H][MAX_W];
set<string> cnt;

void solve(){
    ans=""; cnt.clear();
    for (int i=0;i<h;++i){
        for (int j=0;j<w;++j){
            for (int k=0;k<8;++k){
                for (int l=0;l<h;++l)
                    for (int m=0;m<w;++m)
                        visited[l][m]=0;
                x=i,y=j; now="";
                while(!visited[x][y]){
                    now+=S[x][y]; visited[x][y]=1;
                    (x+=dx[k]+h)%=h; (y+=dy[k]+w)%=w;
                    if (!cnt.count(now)){cnt.emplace(now); continue;}
                    if (ans.size()<now.size()) ans=now;
                    else if (ans.size()==now.size()&&now<ans) ans=now;
                }
            }
        }
    }
    if (ans.size()<2) cout << 0 << '\n';
    else cout << ans << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> h >> w,h){
        for (int i=0;i<h;++i) cin >> S[i];
        solve();
    }
}

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

int gcd(int a, int b){
    while(a != b){
        if(a > b) a = a - b; else b = b - a;
    }
    return a;
}

int lcm(int a, int b){
    return ((a / gcd(a, b)) * b);
}

int main(){
    int h, w;
    while(true){
        char torus[10][20];
        string str[2048];
        bool ok[2048];
        int num = 0;

        for(int i=0; i<2048; ++i){
            str[i] = "";
            ok[i] = true;
        }

        cin >> h >> w;
        if(h==0 && w==0) break;

        int cml = lcm(w, h);

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

        //一番長い文字列をつくる

        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<h; ++i){
                    str[num] += torus[(y+i)%h][x];
                    str[num+1] += torus[h-(y+i)%h-1][x];
                }
                num += 2;
            }
        }
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<w; ++i){
                    str[num] += torus[y][(x+i)%w];
                    str[num+1] += torus[y][w-(x+i)%w-1];
                }
                num += 2;
            }
        }
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<cml; ++i){
                    str[num] += torus[(y+i)%h][(x+i)%w];
                    str[num+1] += torus[(y+i)%h][w-(x+i)%w-1];
                    str[num+2] += torus[h-(y+i)%h-1][(x+i)%w];
                    str[num+3] += torus[h-(y+i)%h-1][w-(x+i)%w-1];
                }
                num += 4;
            }
        }


        vector<char> result;

        sort(str, str+num);

        for(int i=num-1; i>0; --i){
            int length = 0;
            vector<char> k;
            for(int j=0; j<min(str[i].length(), str[i-1].length()); ++j){
                if(str[i][j] == str[i-1][j]) k.push_back(str[i][j]); else break;
            }
            if(k.size() >= result.size()){
                result = k;
            }
        }

        if(result.size() == 1){
            cout << "0" << endl;
        }else{
            vector<char>::iterator it = result.begin();
            while( it != result.end()){
                cout << *it;
                ++it;
            }
            cout << endl;
        }
    }

    return 0;
}