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 <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 j=num-1; j>0; --j){
            if(str[j][0] != str[j-1][0]){
                ok[j] = false;
            }
        }

        for(int i=1; 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][i] == str[j-1][i]){
                        result = str[j];
                        result.resize(i+1);
                    }else{
                        ok[j] = false;
                    }
                }
            }
        }

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

    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
#define REP(i,n) FOR(i,0,n)

int dy[]={-1,-1,-1,0,1,1,1,0};
int dx[]={-1,0,1,1,1,0,-1,-1};
int main(){
  int h,w;
  while(cin>>h>>w,h|w){
    vs f(h);
    REP(i,h){
      cin>>f[i];
    }
    string ans;
    map<string,int> cnt;
    REP(i,h){
      REP(j,w){
        REP(d,8){
          int y=i,x=j;
          string tmp;
          do{
            tmp+=f[y][x];
            if(tmp.size()>=2){
              cnt[tmp]++;
              if(cnt[tmp]>=2){
                if(ans.size()<tmp.size()||(ans.size()==tmp.size()&&ans>tmp)){
                  ans=tmp;
                }
              }
            }
            y+=dy[d];
            x+=dx[d];
            if(y<0)y=h-1;
            if(x<0)x=w-1;
            if(y>=h)y=0;
            if(x>=w)x=0;
          }while(y!=i||x!=j);
        }
      }
    }
    if(ans.size()){
      cout<<ans<<endl;
    }else{
      cout<<0<<endl;
    }
  }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<map>
#define f first
#define s second
using namespace std;

int h,w,ii,jj;
int X[8]={-1,1,-1,1,0,-1,0,1};
int Y[8]={-1,1,1,-1,1,0,-1,0};
char Donut[10][20];
vector<string> V;
string maxs;
map<string,pair<int,int> > M;

void Check(string s)
{
  if(maxs.length()<s.length())maxs=s;
  else if(maxs.length()==s.length()){
    if(maxs>s)maxs=s;
  }
}

void make(int x,int y)
{
  for(int k=0;k<8;k++){
    string S="";
    int a=x,b=y;
    S+=Donut[a][b];
    while(1){
      a+=Y[k];
      b+=X[k];
      if(a==-1)a=h-1;
      if(b==-1)b=w-1;
      if(a==h)a=0;
      if(b==w)b=0;
      if(a==x && b==y)break;
      S+=Donut[a][b];
      if(M[S].f==0 && M[S].s==0){
	M[S].f=x+1;
	M[S].s=y+1;
      }
      else {
	Check(S);
      }
    }
  }
}

int main()
{
  while(1){
    cin>>h>>w;
    if(h+w==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++)cin>>Donut[i][j];
    }
    M.clear();
    maxs="";
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	make(i,j);
      }
    }
    if(maxs=="")cout<<0<<endl;
    else cout<<maxs<<endl;
  }
  return 0;
}

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;
vector<vector<int>> usage;

void solve(int i, int j) {
	for (int k = 0; k < 8;k++) {
		usage.clear();
		usage.resize(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.clear();
		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);
			}
		}
		if (ans.length() < 2) cout << 0 << endl;
		else cout << ans << endl;
	}

}