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<algorithm>
#include<map>
using namespace std;
int dy[8]={-1,-1,0,1,1,1,0,-1};
int dx[8]={0,1,1,1,0,-1,-1,-1};
char t[20][20];
map<string,int> u;
int h,w;
bool visited[20][20];

void func(int y,int x,int d){
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      visited[i][j]=false;
  string str="";
  while(!visited[y][x]){
    visited[y][x]=true;
    str.push_back(t[y][x]);
    u[str]++;
    int ny=y+dy[d],nx=x+dx[d];
    if(ny<0)ny=h-1;
    if(ny>=h)ny=0;
    if(nx<0)nx=w-1;
    if(nx>=w)nx=0;
    y=ny;x=nx;
  }
}

int main(){
  while(1){
    cin>>h>>w;
    if(h==0&&w==0)break;
    u.clear();
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>t[i][j];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	for(int k=0;k<8;k++)
	  func(i,j,k);
    string ans="";
    map<string,int> :: iterator it;
    for(it=u.begin();it!=u.end();it++){
      if(it->second<2)continue;
      if(it->first.size()>ans.size())ans=it->first;
      else if(it->first.size()==ans.size()&&it->first<ans)ans=it->first;
    }
    if((int)ans.size()<2)cout<<0<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
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 <string> 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];
	    if(buf.size()>=mal){
	      if(find(li.begin(),li.end(),buf)!=li.end()) {
		mal=buf.size();
		if(buf.size()>mal) {
		  ans.clear();
		  ans.push_back(buf);
		  int a=0;
		  while(li[a].size()!=mal) a++;
		  li.erase(li.begin(),li.begin()+a-1);
		}else{
		  ans.push_back(buf);
		}
		
	      }else{
		li.push_back(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;
	  }
	}
      }
    }
    //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 <string>
#include <map>
using namespace std;

int main(){
  int h,w;
  string s[10],t;
  map<string,int> v;
  while(1){
    cin>>h>>w;
    if(!h&&!w) break;
    for(int i=0;i<h;i++) cin>>s[i];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	for(int k=-1;k<=1;k++){
	  for(int l=-1;l<=1;l++){
	    if(!k&&!l) continue;
	    int y=i,x=j;
	    t="";
	    t+=s[y][x];
	    while(1){
	      y+=k;
	      x+=l;
	      if(y<0) y+=h;
	      if(x<0) x+=w;
	      if(h<=y) y-=h;
	      if(w<=x) x-=w;
	      if(y==i&&x==j) break;
	      t+=s[y][x];
	      if(v.find(t)!=v.end()) v[t]++;
	      else v.insert(make_pair(t,0));
	    }
	  }
	}
      }
    }
    int ans=0;
    for(map<string,int>::iterator it=v.begin();it!=v.end();it++){
      string z=it->first;
      if(it->second>0&&ans<z.size()) ans=z.size();
    }
    if(ans>=2){
      for(map<string,int>::iterator it=v.begin();it!=v.end();it++)
	if(it->second>0&&ans==it->first.size()){
	  cout<<it->first<<endl;
	  break;
	}
    }else cout<<0<<endl;
    v.clear();
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#define REP(i,a,b) for (int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

int H, W;
int dw[] = {1,1,0,-1,-1,-1,0,1};
int dh[] = {0,1,1,1,0,-1,-1,-1};

int main(void){
    while (cin >> H >> W && H) {
        vector<string> S(H,"");
        rep(i, H) cin >> S[i];
        string ans = "";
        set<string> appear;
        
        rep(h, H) rep(w, W) rep(i, 8) {
            string s = "";
            s += S[h][w];
            int iw = w, ih = h;
            w = (w+dw[i]+W)%W; h = (h+dh[i]+H)%H;
            while (!(w == iw && h == ih)) {
                s.append(1, S[h][w]);
                if (s.length() >= ans.length() && appear.find(s) != appear.end()) {
                    if (s.length()  > ans.length()) ans = s;
                    if (s.length() == ans.length()) ans = min(ans, s);
                }
                appear.insert(s);
                w = (w+dw[i]+W)%W; h = (h+dh[i]+H)%H;
            }
        }
        cout << (ans.length() > 1 ? ans:"0") << endl;
    }
    return 0;
}