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 <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>
#include <array>
using namespace std;
#define rep(i,n) for (int i=0; i < (n); i++)


typedef long long int ll;
typedef double D;      // ??§?¨?????????????double???long double?????????
typedef complex<D> P;  // Point
typedef pair<P, P> L;  // Line
typedef vector<P> VP;


const int INF = (1<<30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n,m) ((n) < (m) + EPS)
#define GE(n,m) ((n) + EPS > (m))
#define EQ(n,m) (abs((n)-(m)) < EPS)

int H, W;
vector<string> field;
int dh[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dw[] = {0, 1, 1, 1, 0, -1, -1, -1};

void func(int h, int w, int d, map<string, int>& memo){
    int sh = h, sw = w;
    string s = "";
    
    do{
        s += field[h][w];
        if(s.length() >= 2)
            memo[s]++;
        h = (h+dh[d]+H)%H;
        w = (w+dw[d]+W)%W;
    }while(sh!=h || sw!=w);
}
void solve(){
    map<string, int> memo;
    
    for(int h=0; h<H; h++){
        for(int w=0; w<W; w++){
            for(int i=0; i<8; i++){
                func(h, w, i, memo);
            }
        }
    }
    
    string ans = "";
    for(auto p: memo){
        if(p.second < 2) continue;
        
        if(ans.length() < p.first.length() || (ans.length() == p.first.length() && p.first < ans))
            ans = p.first;
    }
    if(ans.length() == 0)
        cout << 0 << endl;
    else
        cout << ans << endl;
}

//g++ -std=c++0x -msse4.2 -O3
//#include <bits/stdc++.h>
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout.precision(16);
    //cout.setf(ios::fixed);
    
    while(cin>>H>>W, H|W){
        field = vector<string>(H);
        
        for(int i=0; i<H; i++)
            cin >> field[i];
        solve();
    }
    
    return 0;
}

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 <iostream>
#include <map>
#include <string>
using namespace std;
int w,h;
string ans;
string ptn[30];
int di[] = {0,1,0,-1, 1,1,-1,-1};
int dj[] = {1,0,-1,0, 1,-1,-1,1};

int main(){
  string s;
  while( cin >> h >> w && (h|w) ){
    for( int i=0;i<h;i++ ){
      cin >> ptn[i];
    }
    ans="";
    map<string,int> mp;
    for( int st=0;st<h;st++ ){
      for( int en=0;en<w;en++ ){
	for( int lp=0;lp<8;lp++ ){
	  int i=st, j=en;
	  //	  cout << ptn[i][j] << endl;
	  string s = ptn[i].substr(j,1);
	  while( true ){
	    i+=di[lp]; j+=dj[lp];
	    if( i==h ) i=0;
	    else if( i==-1) i=h-1;
	    if( j==w ) j=0;
	    else if( j==-1 ) j=w-1;
	    if( i==st && j==en )
	      break;
	    s += ptn[i][j];
	    if( s.length() < ans.length() )
	      continue;
	    mp[s]++;
	    if( mp[s]==2 ){
	      if( s.length() > ans.length() )
		ans = s;
	      else if( s.length()==ans.length() && s < ans )
		ans = s;
	    }
	  }
	}	  
      }
    }
    if( ans.length()<2 )
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

int W, H;
string mat[10][20];
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<string> strs;
int oi, oj;

void dfs(int i, int j, int k, string str){
	int ni = (i+dx[k]+H)%H; int nj = (j+dy[k]+W)%W;
	if(ni == oi && nj == oj) { strs.push_back(str); return; }
	str.append(mat[ni][nj]);	
	dfs(ni, nj, k, str);
	return;
}

int main(){
	while(cin >> H >> W and W + H){
		strs.clear();
		REP(i, H) {
			string str;
			cin >> str;
			REP(j, W) mat[i][j] = string(&str[j], 1);
		}
		REP(i, H) REP(j, W) REP(k, 8){
			oi = i; oj = j;
			dfs(i, j, k, mat[i][j]);
		}
		sort(ALL(strs));
		int rcnt = 0;
		string res;
		REP(i, strs.size()-1){
			int cnt = 0;
			REP(j, min(strs[i].size(), strs[i+1].size())){
				if(strs[i][j] != strs[i+1][j]) break;
				cnt++;
			}
			if(cnt > rcnt) { rcnt = cnt; res = strs[i].substr(0, rcnt); }
		}
		if(rcnt < 2) cout << "0" << endl;
		else cout << res << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<map>
using namespace std;

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int main(){
  int h,w;
  string g[10];
  int dx[] = {0,1,1,1,0,-1,-1,-1},dy[] ={-1,-1,0,1,1,1,0,-1};
  int lm;

  while(cin>> h >> w &&(h||w)){
    for(int i=0;i<h;i++)cin >> g[i];

    map<string,int> m;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	for(int k=0;k<8;k++){
	  string tmp;
	  tmp += g[i][j];
	  int sx = j,sy = i;
	  if(k&1)lm = lcm(h,w);
	  else if(!(k%4))lm = h;
	  else lm = w;
	  for(int t=1;t<lm;t++){
	    sx = (sx+w+dx[k])%w;
	    sy = (sy+h+dy[k])%h;
	    tmp += g[sy][sx];
	    m[tmp]++;
	  }
	}


    string ans = "A";
    map<string,int>::iterator ite;
    for(ite=m.begin();ite!=m.end();ite++)
      if((*ite).second>1){
	string tmp = (*ite).first;
	if(ans.size()<tmp.size() || (ans.size()==tmp.size()&&ans>tmp))ans = tmp;
      }
    if(ans.size()>1)cout << ans << endl;
    else cout << 0 << endl;
  }
}