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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 2020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcount(x);};
const int mod = 1e9 + 7;
//const int mod = 998244353;

void ch(int &y, int &x, int h, int w){
    y = (y + h) % h;
    x = (x + w) % w;
}

int main(){
    while(1){
        int h,w; cin >> h >> w;
        if(h+w == 0) break;
        vs s(h); rep(i,h) cin >> s[i];
        map<string,int> mp;
        rep(i,h){
            rep(j,w){
                rep(k,8){
                    vb v(26,false);
                    v[s[i][j]-'A'] = true;
                    int y = i;
                    int x = j;
                    string t;
                    while(1){
                        t += s[y][x];
                        mp[t]++;
                        y += dy[k];
                        x += dx[k];
                        ch(y,x,h,w);
                        if(y == i && x == j) break;
                    }
                }
            }
        }
        ll num = 1;
        string ans;
        for(auto i : mp){
            if(i.second >= 2){
                if(num < i.first.size()){
                    num = i.first.size();
                    ans = i.first;
                }
            }
        }
        if(num == 1) cout << 0 << "\n";
        else cout << ans << "\n";
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
 
using namespace std;
 
const int MAXH = 10;
const int MAXW = 20;
int W,H;
char field[MAXH][MAXW];
bool come[MAXH][MAXW];
int dx[] = {0,1,0,-1,1,1,-1,-1};
int dy[] = {1,0,-1,0,1,-1,1,-1};
 
void input(){
  for(int i = 0; i < H; i++)
    for(int j = 0; j < W; j++) cin >> field[i][j];
}
 
string comp(const string& s1, const string& s2){
   
  int len = min(s1.length(),s2.length());
  string s ="";
 
  for(int i = 0; i < len; i++){
    if(s1[i] != s2[i]) return s;
    s+=s1[i];
  }
  return s;
}
 
void print(const vector<string>& v){
  cout << endl;
  for(int i = 0; i < v.size(); i++)
    cout << v[i] << endl;
  cout << endl;
}
 
void solve(){
  vector<string> V;
 
  for(int i = 0; i < H; i++) 
    for(int j = 0; j < W; j++)
      for(int d = 0; d < 8; d++){
        memset(come,false,sizeof(come));
 
        string s = "";
        int x=j,y=i;
 
        while(!come[y][x]){
          s += field[y][x];
          come[y][x] = true;
          y = (y+dy[d]+H)%H;
          x = (x+dx[d]+W)%W;
        }
 
        V.push_back(s);
      }
 
  sort(V.begin(),V.end());
  // print(V);
  
  string ans = "";
 
  for(int i = 1; i < V.size(); i++){
     
    string tmp = comp(V[i-1],V[i]);
     
    if(tmp.length() == ans.length()) ans = min(ans,tmp);
    else if(tmp.length() > ans.length()) ans = tmp;
  }
   
  if(ans.length() > 1)cout << ans << endl;
  else cout << "0" << endl;
}
 
int main(){
 
  while(cin >> H >> W && H+W){
    input();
    solve();
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <complex>
#include <set>
#include <map>
#include <cmath>
using namespace std;


int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};

int main(){
	int H,W;
	while(cin >> H >> W && H ){
		vector<string> s(H);
		for(int i = 0 ; i < H ; i++) cin >> s[i];
		
		
		map<string,int> answer;
		for(int i = 0 ; i < H ; i++){
			for(int j = 0 ; j < W ; j++){
				for(int d = 0 ; d < 8 ; d++){
					string wow;
					int done[20][20] = {};
					int x = j , y = i;
					while( done[y][x] == 0 ){
						wow += s[y][x];
						done[y][x] = true;
						answer[wow]++;
						x += dx[d];
						y += dy[d];
						x += W;
						y += H;
						x %= W;
						y %= H;
					}
				}
			}
		}
		int len = 0;
		for( map<string,int>::iterator it = answer.begin(); it != answer.end() ; it++){
			if( it->second < 2 || it->first.size() < 2 ) continue;
			len = max( len , (int)it->first.size() );
		}
		int flag = 0;
		for( map<string,int>::iterator it = answer.begin(); it != answer.end() ; it++){
			if( it->second < 2 || it->first.size() < 2 ) continue;
			if( len == it->first.size() ){
				cout << it->first << endl;
				flag = true;
				break;
			}
		}
		if(flag==0)cout << 0 << endl;
		
	}
}