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<map>
#include<vector>
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;
    vector<string> v;
    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]++;
	    v.push_back(tmp);
	  }
	}
    string ans = v[0];
    for(int i=1;i<(int)v.size();i++)
      if(m[v[i]]>1)
	if(ans.size()<v[i].size() || (ans.size()==v[i].size()&&ans>v[i]))ans = v[i];
    if(m[ans]>1)cout << ans << endl;
    else cout << 0 << endl;
  }
}

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<num-1; ++i){
            int length = 0;
            string k = "";
            for(int j=0; j<min(str[i].length(), str[i+1].length()); ++j){
                if(str[i][j] == str[i+1][j]) k += str[i][j]; else break;
            }
            if(length >= result.length()){
                result = k;
            }
        }

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

    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <complex>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <stack>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cassert>
#include <numeric>
using namespace std;
typedef long long ll;
typedef pair<int,int> PI;
#define EPS (1e-6)
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define REP(i, n) rep (i, n)
#define F first
#define S second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define min3(a,b,c) min((a),min((b),(c)))
#define min4(a,b,c,d) min((a),min3((b),(c),(d)))
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(),a.end()
#define FLL(a,b) memset((a),b,sizeof(a))
#define CLR(a) memset((a),0,sizeof(a))
#define FOR(it,a) for(__typeof(a.begin())it=a.begin();it!=a.end();++it)
template<typename T,typename U> ostream& operator<< (ostream& out, const pair<T,U>& val){return out << "(" << val.F << ", " << val.S << ")";}
template<class T> ostream& operator<< (ostream& out, const vector<T>& val){out << "{";rep(i,SZ(val)) out << (i?", ":"") << val[i];return out << "}";}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt,pt> line;
FP dot(P a,P b){return real(conj(a)*b);}
FP crs(P a,P b){return imag(conj(a)*b);}
P ortho(P a){return conj(P(imag(a),real(a)));}
P ortho(line a){return ortho(a.S-a.F);}
P crspt(P a,P b,P c,P d){b-=a,d-=c;return a+b*crs(d,c-a)/crs(d,b);}
P crspt(line a,line b){return crspt(a.F,a.S,b.F,b.S);}
bool onl(P a1,P a2,P b){return abs(b-a1)+abs(b-a2)<abs(a1-a2)+EPS;}
bool onl(line a,P b){return onl(a.F,a.S,b);}
bool iscrs(line a,line b){P c=crspt(a,b);return onl(a,c)&&onl(b,c);}
void pkuassert(bool t){t=1/t;};
int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,-1,1,1,-1};
enum{TOP,BTM,LFT,RGT,FRT,BCK};
int dxdy2ce[]={RGT,FRT,LFT,BCK};
template<class T> T shift(T a,int b,int c,int d,int e){
 __typeof(a[0])t=a[b];a[b]=a[c];a[c]=a[d];a[d]=a[e];a[e]=t;
 return a;}
template<class T> T rgt(T a){return shift(a,TOP,LFT,BTM,RGT);}
template<class T> T lft(T a){return shift(a,TOP,RGT,BTM,LFT);}
template<class T> T frt(T a){return shift(a,TOP,BCK,BTM,FRT);}
template<class T> T bck(T a){return shift(a,TOP,FRT,BTM,BCK);}
line mkl(P a,P v){return line(a,a+v);}
FP lpdist(line a,P b){return abs(b-crspt(a,mkl(b,ortho(a))));}
FP spdist(line a,P b){
  P c(crspt(a,mkl(b,ortho(a))));
  return onl(a,c)?abs(b-c):min(abs(a.F-b),abs(a.S-b));
}
FP ssdist(line a,line b){
 return
  iscrs(a,b)?0.:
   min4(spdist(a,b.F),spdist(a,b.S),
        spdist(b,a.F),spdist(b,a.S));
}
namespace std{
  bool operator<(const P&a,const P&b){
    return mp(real(a),imag(a))<mp(real(b),imag(b));
  }
}

int main(int argc, char *argv[])
{
  int R, C;
  while(cin >> R >> C, C) {
    char A[30][30] = {};
    rep(i, R) rep(j, C) cin >> A[i][j];
    map<string, int> M;
    rep(i, R) rep(j, C) {
      rep(k, 8) {
        string s;
        int ai = i, aj = j;
        do {
          s += A[ai][aj];
          M[s] += 1;
          ai += dx[k];
          aj += dy[k];
          ai = (ai+R)%R;
          aj = (aj+C)%C;
        } while(ai != i || aj != j);
      }
    }
    string ans;
    FOR(it, M) {
      //cout << it->first << endl;
      if (it->second >= 2 && SZ(ans) < SZ(it->first))
        ans = it->first;
    }
    if (SZ(ans)<2) cout << 0 << endl;
    else
      cout << ans<< endl;
  }



  return 0;
}