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<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;
			}
		}
		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+1文字目とj-1のi+1文字目を比べるよ
					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;
}

C++: #include <cstdio>
#include <string>
#include <set>
#include <vector>
using  namespace std;

int dr[]={-1, -1, -1, 0, 0, 1, 1, 1};
int dc[]={-1, 0, 1, -1, 1, -1, 0, 1};

bool cmp(const string& a, const string& b)
{
    if (a.size()<b.size()) return 1;
    if (a.size()>b.size()) return 0;
    return a>b;
}

int main()
{
    int n, m;
    while (scanf("%d%d", &n, &m), n) {
        char s[11][21];
        for(int i=0;i<n;++i) scanf("%s", s[i]);

        int maxlen=0;
        vector<string> spells[200];
        for(int i=0;i<n;++i) for(int j=0;j<m;++j) for(int k=0;k<8;++k) {
            int r=i, c=j, q=0;
            string t;
            do {
                t+=s[r][c];
                spells[q++].push_back(t);
                r=(r+dr[k]+2*n)%n;
                c=(c+dc[k]+2*m)%m;
            } while (r!=i || c!=j);
            maxlen=max(maxlen, q);
        }

        string res;
        for(int k=1;k<maxlen;++k) {
            int N=spells[k].size();
            set<string> v;
            for(int i=0;i<N;++i) {
                if (v.count(spells[k][i]) and cmp(res, spells[k][i]))
                    res=spells[k][i];
                v.insert(spells[k][i]);
            }
        }
        puts(res.size()?res.c_str():"0");
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
	int w, h;
	char seat[10][20];
	map<string, int> mp;

	int dx[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
	int dy[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
	
	while( cin >> h >> w, (h||w))
	{
		mp.clear();
		
		for( int j = 0; j < h; j++)
			for( int i = 0; i < w; i++)
			{
				cin >> seat[j][i];
			}
		
		
		for( int j = 0; j < h; j++)
			for( int i = 0; i < w; i++)
				for( int d = 0; d < 8 ; d++ )
				{ 
					string str = "";
					int x = i, y = j;
					
					do
					{
						str += seat[y][x];

						x = (x + w + dx[d]) % w;
						y = (y + h + dy[d]) % h;

						mp[str] += 1;

					}
					while( x != i || y != j );
					
				}
	
		string out = "";
		map<string, int>::iterator it; 

		for( it = mp.begin() ; it != mp.end(); it++ )
		{
			if( (*it).second >= 2 )
				if( (*it).first.length() > out.length() )
				{
					out = (*it).first;
				}
				else if( (*it).first.length() == out.length() )
				{
					if( out > (*it).first )
						out = (*it).first;
				}
		}

		if( out.length() >= 2 )
			cout << out << endl;
		else
			cout << '0' << endl;
	}
	
	return 0;
}