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<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>

using namespace std;

#define INF 999999999
#define REP(i,n) for(int i=0; i<(int)(n); i++)

vector<string> donut(10);
vector<string> lis(200);

int main(){
	int i, j, w, h;
	int d[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
	string ans, ins;
	while(1){
		cin >> h >> w;
		if(!h) break;
		ans="0";
		lis.clear();
		donut.clear();
		REP(a,h){
			cin >> ins;
			donut.push_back(ins);
		}
		/*REP(a,h){
			cout << donut[a] << endl;
		}*/
		REP(a,h){
			REP(b,w){
				REP(c,8){
					//cout << a << "," << b << ":" << donut[a][b] << endl;
					ins = donut[a][b];
					//cout << ins << endl;
					int x = a+d[c][0], y = b+d[c][1];
					if(x<0) x = h-1;
					if(x>h-1) x = 0;
					if(y<0) y = w-1;
					if(y>w-1) y = 0;
					while(x!=a||y!=b){
						//cout<<a<<","<<b<<"("<<c<<"):"<<x<<","<<y<<"->";
						//cout<<donut[y][x]<<endl;
						ins += donut[x][y];
						x = x+d[c][0];
						y = y+d[c][1];
						if(x<0) x = h-1;
						if(x>h-1) x = 0;
						if(y<0) y = w-1;
						if(y>w-1) y = 0;
					}
					lis.push_back(ins);
				}
			}
		}
		//REP(a,lis.size()) cout << lis[a] << endl;
		REP(a,lis.size()){
			for(i=a+1;i<lis.size();i++){
				if(lis[a][0]==lis[i][0]){
					//cout << a << "-" << i << endl;
					for(j=max(2,(int)ans.size());j<=min(lis[a].size(),lis[i].size());j++){
						if(lis[a].substr(0,j)==lis[i].substr(0,j)){
							ins = lis[a].substr(0,j);
							if(ins.size() > ans.size()) ans = ins;
							else if(ins.size()==ans.size()&&ins < ans) ans = ins;
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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() <= 1) ? "0" : result) << endl;

    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct bstr{
  string str;
  bstr(string s){str=s;}
  bool operator<(const bstr& a) const{
    return str.size()==a.str.size() ? (str.compare(a.str) < 0): str.size() > a.str.size();
  }
};
int main(){
  int h,w;
  int i,j,k;
  int ai[]={ 1, 1, 1, 0, 0,-1,-1,-1};
  int aj[]={ 1, 0,-1, 1,-1, 1, 0,-1};
  cin >> h >> w;
  while(h!=0){
    string str[10];
    for(i=0;i<h;i++) cin >> str[i];
    vector <bstr> li;
    vector <string> ans;
    int mal=1;
    for(i=0;i<h;i++){
      //cout << i << endl;
      for(j=0;j<w;j++){
	for(k=0;k<8;k++){
	  int bi=i,bj=j;
	  string buf;
	  buf += str[i][j];
	  bi+=ai[k];bj+=aj[k];
	  if(bi<0) bi=h-1;
	  if(bi>=h) bi=0;
	  if(bj<0) bj=w-1;
	  if(bj>=w) bj=0;
	  while(bi!=i||bj!=j){
	    buf+=str[bi][bj];
	    li.push_back(bstr(buf));
	    
	    bi+=ai[k];bj+=aj[k];
	    if(bi<0) bi=h-1;
	    if(bi>=h) bi=0;
	    if(bj<0) bj=w-1;
	    if(bj>=w) bj=0;
	  }
	}
      }
    }
    sort(li.begin(),li.end());
    for(i=0;i<li.size()-1;i++){
      if(li[i].str==li[i+1].str) {
	ans.push_back(li[i].str);
	break;
      }
    }
    //for(i=0;i<li.size();i++) cout << li[i] << endl;
    sort(ans.begin(),ans.end());
    //for(i=0;i<ans.size();i++) cout << ans[i] << endl;
    if(ans.size()!=0) cout << ans[0] << endl;
    else cout << 0 << endl;
    cin >> h >> w;
  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <set>
using namespace std;
#define REP(i,x)for(__typeof(x) i=0;i<x;i++)

string res;
set<string> m;
int H,W,dx,dy,min_len;
char s[200];
string MAP[11];
bool visit[20][20];
void dfs(int x,int y,int k){
  if(k==min_len){
    if(m.count(string(s)))res=min(res,string(s));
    m.insert(string(s));
  }else if(k>min_len){
    if(m.count(string(s)))res=string(s),min_len=k;
    m.insert(string(s));
  }
  m.insert(string(s));
  if(visit[y][x])
    return;
  s[k]=MAP[y][x];  visit[y][x]=1;
  dfs((x + dx + W) % W, (y + dy + H) % H, k + 1);
  s[k]=0;  visit[y][x]=0;
}
int main() {
  while(cin>>H>>W,H){
    REP(i,H)cin>>MAP[i];
    m.clear();
    min_len=1; res="0";
    REP(y,H)REP(x,W){
      for(dx=-1;dx<=1;dx++){
        for(dy=-1;dy<=1;dy++){
          if(dx|dy)dfs(x,y,0);
        }
      }
    }
    cout<<res<<endl;
  }
}