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 <stdio.h>
#include <vector>
#include <algorithm>

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} };

void search( vector<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]];
		v->push_back(str);

		while( true ) {
			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]];
			v->push_back(str);
		}
		

	}
}

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	while(1){
		cin >> l[0] >> l[1];
		if( l[0] == 0 && l[1] == 0 ) break;
		char donut[10][20];

		vector< string > strs;

		//入力を取得する
		for(int i = 0; i < l[0]; i++) {
			string str;
			cin >> str;
			for(int j = 0; j < l[1]; j++){
				donut[i][j] = str[j];
			}
		}

		for(int i = 0; i < l[0]; i++) {
			for(int j = 0; j < l[1]; j++) {
				int pos[2];
				pos[0] = i;
				pos[1] = j;
				search( &strs, pos, donut );
			}
		}
		sort( strs.begin(), strs.end() );

		string ans = "";
		for(int i = 0; i < strs.size(); i++) {
			for(int j = i + 1; j < strs.size(); j++) {
				if( strs[i] == strs[j] && ans.length() < strs[i].length() && strs[i].length() > 1) {
					ans = strs[i];
				}
			}
		}
		if(ans == "") 
			cout << 0 << endl;
		else 
			cout << ans << endl;

	}
	//while(1){}
	return 0;
}

C++: #include <map>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
int m[29][29];
main(){
	string s,t;
	int h,w,i,j,x,y,X,Y;
	for(;cin>>h>>w,h;cout<<s<<endl){
		vector<string>v;
		for(i=0;i<h;i++)cin>>s,v.push_back(s);
		s="0";
		map<string,int>M;
		for(i=0;i<h;i++)for(j=0;j<w;j++)for(x=-1;x<=1;x++)for(y=-1;y<=1;y++)if(x||y){
			memset(m,0,sizeof(m));
			Y=i,X=j;
			t=v[Y][X],m[Y][X]=1;X=(X+x+w)%w,Y=(Y+y+h)%h;
			for(;!m[Y][X];X=(X+x+w)%w,Y=(Y+y+h)%h){
				t+=v[Y][X],m[Y][X]=1;
				if(++M[t]>1 && (s.size()<t.size()||(s.size()==t.size()&&s>t)))s=t;
			}
		}
	}
}

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

int h,w;
char fie[11][21];
int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
bool used[11][21];
vector<string> index2;
string str;

void dfs(int y,int x,int k){
	if(used[y][x])return;
	used[y][x]=true;
	str+=fie[y][x];
	index2.push_back(str);
	dfs((y+dy[k]+h)%h,(x+dx[k]+w)%w,k);
}

int main(void){
	while(1){
		scanf("%d %d%*c",&h,&w);
		if(h+w==0)break;
		index2.clear();
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				scanf("%c",&fie[i][j]);
			}
			scanf("%*c");
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				for(int k=0;k<8;k++){
					memset(used,false,sizeof(used));
					str="";
					dfs(i,j,k);
				}
			}
		}
		sort(index2.begin(),index2.end());
		int res=index2.size();
		index2.push_back("0");
		for(int i=0;i<index2.size();i++){
			if(index2[i].size()>=2){
				if(index2[i]==index2[i-1] && index2[res].size()<index2[i].size())res=i;
			}
		}
		cout << index2[res] << endl;
	}
	return 0;
}

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;
}