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;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

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

int main(void) {
  int64 h, w;
  while (cin >> h >> w && h +w) {
    vector<string> s(h);
    REP(i, h) cin >> s[i];
    map<string, int> mp;
    REP(sy, h) {
      REP(sx, w) {
        mp[string(1, s[sy][sx])]++;
        REP(d, 8) {
          int64 yy = sy, xx = sx;
          vector<vector<int>> used(h, vector<int>(w, 0));
          string t(1, s[sy][sx]);
          while (!used[yy][xx]) {
            used[yy][xx] = 1;
            yy = (yy + dy[d] + h) % h; xx = (xx + dx[d] + w) % w;
            if (!used[yy][xx]) {
              t += s[yy][xx];
              mp[t]++;
            }
          }
        }
      }
    }
    string res = "";
    for (auto& x : mp) {
      if (x.sc > 1) {
        if (res.size() < x.fs.size()) {
          res = x.fs;
        } else if (res.size() == x.fs.size()) {
          if (res >= x.fs) {
            res = x.fs;
          }
        }
      }
    }
    if (res.size() == 0) cout << 0 << endl;
    else cout << res << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <set>
#include <algorithm>

using namespace std;

#define dirmax 8
int l[2];
int dir[8][2] = { {0,1}, {1,0}, {-1,0}, {0,-1}, {1,1}, {-1,1}, {1,-1}, {-1,-1} };
string ans;

void search( set<string> *v, int pos[2] , char ch[10][20]){

	for(int i = 0; i < dirmax; i++) {
		string str;
		int cpos[] = { pos[0], pos[1] };

		str += ch[pos[0]][pos[1]];

		while( true ) {
			for(int j = 0; j < 2; j++) {
				cpos[j] += dir[i][j];
				if( cpos[j] < 0 ) cpos[j] = l[j] - 1;
				if( cpos[j] >= l[j] ) cpos[j] = 0;
			}
			if( cpos[0] == pos[0] && cpos[1] == pos[1] ) break;
			str += ch[cpos[0]][cpos[1]];
			
			if( v->find(str) != v->end() ) {
				if( ( str.length() > ans.length() ) || ( str.length() == ans.length() && str < ans ) ) {
					ans = str;
				}
			}
			v->insert(str);
		}
		

	}
}

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);
	while(1){
		cin >> l[0] >> l[1];
		if( l[0] == 0 && l[1] == 0 ) break;
		char donut[10][20];
		ans = "";

		set< string > strs;

		//入力を取得する
		for(int i = 0; i < l[0]; i++) {
			string str;
			cin >> str;
			for(int j = 0; j < l[1]; j++){
				donut[i][j] = str[j];
			}
		}

		for(int i = 0; i < l[0]; i++) {
			for(int j = 0; j < l[1]; j++) {
				int pos[2];
				pos[0] = i;
				pos[1] = j;
				search( &strs, pos, donut );
			}
		}

		if(ans == "") 
			cout << 0 << endl;
		else 
			cout << ans << endl;

	}
	//while(1){}
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

#define x first
#define y second

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

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

int main(void){
    for(int w, h; cin >> h >> w, h;){
        vs strs(h);
        rep(y, h){
            cin >> strs[y];
        }

        set<string> s;
        string res = "0";
        rep(y, h){
            rep(x, w){
                rep(i, 8){
                    string cur;
                    pii next = mp(x, y);

                    do{
                        cur.pb(strs[next.y][next.x]);
                        if(1 < cur.size() && !s.insert(cur).second)
                        {
                            if((res.size() < cur.size()) || 
                               (res.size() == cur.size() && cur < res)){
                                res = cur;
                            }
                        }

                        next.x = (next.x + dx[i] + w) % w;
                        next.y = (next.y + dy[i] + h) % h;
                        debug(cur);
                        
                    } while(next != mp(x, y));
                }
            }
        }

        cout << res << endl;
    }

	return 0;
}