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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

string m[1000];
string ans;
int h, w;
map<string, int> msi;

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

string big(string a, string b){
    if(a.size() > b.size()) return a;
    else if(a.size() < b.size()) return b;
    else{
        if(a < b) return a;
        else return b;
    }
}

void search(int y, int x){
    rep(i,8){
        string s;
        int j = 0;
        while(true){
            s += m[y + dy[i] * j][x + dx[i] * j];
            msi[s]++;
            if(msi[s] == 2){
                ans = big(ans, s);
            }
            j++;
            if(dy[i] * j % h == 0 && dx[i] * j % w == 0) break;
        }
    }
}

int main(){
    while(cin >> h >> w, h||w){
        rep(i,1000) m[i] = "";
        ans = "";
        msi.clear();
        rep(i,h){
            string s;
            cin >> s;
            rep(j,50) rep(k,50) m[i + j * h]+=s;
        }
        for(int i = h * 25; i < h * 26; i++){
            for(int j = w * 25; j < w * 26; j++){
                search(i,j);
            }
        }
        if(ans == "") cout << 0 << endl;
        else cout << ans << endl;
    }
}

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


        string result = "";

        sort(str, str+num);

        //徐々に削る
        for(int i=0; i<cml; ++i){
            for(int j=num-1; j>0; --j){
                if(ok[j] && str[j].length() >= i && str[j-1].length() >= i){
                    if(str[j].substr(0, i) == str[j-1].substr(0, i)){
                        result = str[j];
                        result.resize(i);
                    }else{
                        ok[j] = false;
                    }
                }
            }
        }

        cout << ((result.length() == 0) ? "0" : result) << endl;

    }

    return 0;
}

C++: #include<iostream>
#include<map>
#include<cstring>
using namespace std;

bool used[10][20];

int main(){
    int h, w;
    while(cin >> h >> w, h+w){
        char mat[h][w];
        for(int i = 0; i < h; i++)  for(int j = 0; j < w; j++)  cin >> mat[i][j];
        map<string,int> cnt;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                for(int di = -1; di <= 1; di++){
                    for(int dj = -1; dj <= 1; dj++){
                        if(di == 0 && dj == 0)  continue;
                        memset(used, 0, sizeof(used));
                        string acc = "";
                        int si = i, sj = j;
                        while(!used[si][sj]){
                            acc += mat[si][sj];
                            cnt[acc]++;
                            used[si][sj] = true;
                            si += di, sj += dj;
                            si = (si + h) % h;
                            sj = (sj + w) % w;
                        }
                    }
                }
            }
        }
        string ans = "0";
        for(pair<string,int> p : cnt){
            if(p.second > 1 && p.first.length() > ans.length()) ans = p.first;
        }
        cout << ans << endl;
    }
    return 0;
}