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>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=INT_MAX,MOD=1e9+7;

int main(){
	int dx[8]={0,1,1,1,0,-1,-1,-1};
	int dy[8]={-1,-1,0,1,1,1,0,-1};
	int h,w;
	while(cin>>h>>w&&h){
		char c[h][w]; rep(i,h)rep(j,w) cin>>c[i][j];
		vector<string> s;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				for(int dir=0;dir<8;dir++){
					int nowy=y,nowx=x;
					string tmp;
					while(1){
						tmp+=c[nowy][nowx];
						nowy=(h+nowy+dy[dir])%h;
						nowx=(w+nowx+dx[dir])%w;
						s.push_back(tmp);
						if(nowy==y&&nowx==x) break;
					}
				}
			}
		}
		sort(s.begin(),s.end());
		string ans="0"; int size=1;
		for(int i=0;i<(int)s.size()-1;i++){
			//cout<<s[i]<<endl;
			string tmp;
			if(s[i]==s[i+1]){
				tmp=s[i];
			}
			if(size<(int)tmp.size()){
				ans=tmp;
				size=tmp.size();
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
 
using namespace std;
 
#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif
 
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) { return __builtin_popcountll(n); }
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) { return n ? 1 + popcount(n & (n - 1)) : 0; }
#endif
 
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
 
template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }
 
typedef long long ll;
typedef pair<int, int> pint;
 
template <class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os;  }
 
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
 
 
bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }
 
template <class T> void print(T a, int n, int br = 1, const string& deli = ", ") { cout << "{ "; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << " }"; while (br--) cout << endl; }
template <class T> void print(const vector<T>& v, int br = 1, const string& deli = ", ") { print(v, v.size(), br, deli); }
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) {	if (width != -1) cout.width(width); cout << a[i][j] << ' ';	} cout << endl; } while (br--) cout << endl; }
 
template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T, class U> void input(T& a, U& b, int n) { for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; }

const int mod = 1000000007;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
 


int main()
{
	ios::sync_with_stdio(false);

	int h, w;
	while (cin >> h >> w, h)
	{
		char c[32][32];
		for (int i = 0; i < h; ++i)
			cin >> c[i];

		map<string, int> spe;
		for (int sy = 0; sy < h; ++sy)
		{
			for (int sx = 0; sx < w; ++sx)
			{
				for (int dy = -1; dy <= 1; ++dy)
				{
					for (int dx = -1; dx <= 1; ++dx)
					{
						if (dx == 0 && dy == 0)
							continue;

						string s;
						for (int x = sx, y = sy; ; )
						{
							s += c[y][x];

							++spe[s];

							x = (x + dx + w) % w;
							y = (y + dy + h) % h;
							if (x == sx && y == sy)
								break;
						}
					}
				}
			}
		}

		string res = "0";
		foreach (it, spe)
		{
			if (it->second >= 2)
			{
				const string& t = it->first;
				if (t.size() > res.size() || (t.size() == res.size() && t < res))
					res = t;
			}
		}

		cout << res << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int main()
{
    int h,w;
    int dx[8]={0,1,1,1,0,-1,-1,-1};
    int dy[8]={1,1,0,-1,-1,-1,0,1};
    while(cin >>h >>w,h|w)
    {
        vector<string> s(h);
        rep(i,h) cin >>s[i];

        map<string,int> ct;
        //start
        rep(i,h)rep(j,w)rep(k,8)
        {
            int x=j,y=i;
            string now="";

            vector<vector<int>> vis(h,vector<int>(w,0));
            rep(a,h*w)
            {
                if(vis[y][x]) break;

                now+=s[y][x];
                vis[y][x]=1;
                y=(y+dy[k]+h)%h;
                x=(x+dx[k]+w)%w;

                if(now.size()>=2)
                {
                    if(ct.find(now)==ct.end()) ct[now]=1;
                    else ++ct[now];
                }
            }
        }

        string ans="";
        for(const auto &i:ct)
        {
            if(i.se>1)
            {
                string t=i.fi;
                if(t.size()>ans.size()) ans=t;
                else if(t.size()==ans.size() && t<ans) ans=t;
            }
        }

        if(ans=="") ans="0";
        std::cout << ans << std::endl;
    }
    return 0;
}