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(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int h, w;
		cin >> h >> w;
		if (!h)break;
		vector<string> v(h);
		REP(i, h)cin >> v[i];
		set<string> st;
		string ans = "";
		REP(i, h) {
			REP(j, w) {
				REP(k, 8) {
					int ni = i, nj = j;
					string s;
					s += v[i][j];
					REP(l, INF) {
						if (!st.insert(s).second) {
							if (ans.size() < s.size())ans = s;
							else if (ans.size() == s.size() && ans > s)ans = s;
						}
						ni = (ni + dx[k] + h) % h, nj = (nj + dy[k] + w) % w;
						if (ni == i&&nj == j)break;
						s += v[ni][nj];

					}
				}
			}
		}
		if (ans.size() <= 1)cout << 0 << endl;
		else cout << ans << endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES

#define INF 0x3f3f3f3f

#include <cstdio>

#include <iostream>

#include <sstream>

#include <cmath>

#include <cstdlib>

#include <algorithm>

#include <queue>

#include <stack>

#include <limits>

#include <map>

#include <string>

#include <cstring>

#include <set>

#include <deque>

#include <bitset>

#include <list>

#include <cctype>

#include <utility>

 

using namespace std;

 

typedef long long ll;

typedef pair <int,int> P;

typedef pair <int,P > PP;

 

const static int tx[] = {0,1,1,1,0,-1,-1,-1};

const static int ty[] = {-1,-1,0,1,1,1,0,-1};



int main(){

  int H,W;

  while(~scanf("%d %d",&H,&W)){

    if(H==0 && W==0) break;



    char line[32];

    char stage[10][20];



    for(int y=0;y<H;y++){

      scanf("%s",line);

      for(int x=0;x<W;x++){

	stage[y][x] = line[x];

      }

    }



    string res = "";

    vector<string> spells;

    map<string,int> freq;

    

    for(int y=0;y<H;y++){

      for(int x=0;x<W;x++){

	int sx = x;

	int sy = y;



	for(int dir=0;dir<8;dir++){

	  string spell = "";

	  spell.push_back(stage[sy][sx]);

	  int dx = sx;

	  int dy = sy;

	  for(int i=0;i<1000;i++){

	    dx = tx[dir] + dx;

	    dy = ty[dir] + dy;

	    if(dx < 0) dx = W-1;

	    if(dx >= W) dx = 0;

	    if(dy < 0) dy = H-1;

	    if(dy >= H) dy = 0;



	    if(dy == sy && dx == sx) break;

	    spell.push_back(stage[dy][dx]);

	    freq[spell]++;

	    if(freq[spell] >= 2){

	      if(res.size() < spell.size()

		 || (res.size() == spell.size() && res > spell)){

		res = spell;

	      }

	    }

	  }

	  spells.push_back(spell);

	  // cout << spell << endl;

	}

      }

    }

    printf("%s\n",res != "" ? res.c_str() : "0");

  }

}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <set>

using namespace std;

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

int main(void){
    int h, w;
    while (cin>>h>>w && h) {
        vector<string> s(h);
        for (int i = 0; i < h; i++) cin >> s[i];
        
        set<string> p;
        string ans = "";
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                for (int k = 0; k < 8; k++) {
                    int ny = i, nx = j;
                    string t = "";
                    t += s[ny][nx];
                    while ((ny+dy[k]+h)%h != i || (nx+dx[k]+w)%w != j) {
                        ny = (ny + dy[k] + h) % h;
                        nx = (nx + dx[k] + w) % w;
                        t += s[ny][nx];
                        
                        auto it = p.find(t);
                        if (it == p.end()) p.insert(t);
                        else if (t.size() > ans.size()) ans = t;
                        else if (t.size() == ans.size()) ans = min(ans, t);
                    }
                }
            }
        }
        if (ans.size() == 0) cout << 0 << endl;
        else cout << ans << endl;
    }
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	for (;;) {
		int h, w; cin >> h >> w;
		if (h == 0) break;
		vector<string> a(h);
		for (int i = 0; i < h; i++) cin >> a[i];
		map<string, int> m;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				for (int di = -1; di <= 1; di++)
					for (int dj = -1; dj <= 1; dj++) {
						if (!di && !dj) continue;
						string s;
						int _i = i, _j = j;
						for (;;) {
							s += a[_i][_j];
							m[s]++;
							_i = (_i + di + h) % h;
							_j = (_j + dj + w) % w;
							if (_i == i && _j == j) break;
						}
					}
		string ans;
		for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
			string s = it->first;
			int x = it->second;
			if (x <= 1) continue;
			if (s.length() > ans.length() || (s.length() == ans.length() && s < ans))
				ans = s;
		}
		if (ans.length() < 2) ans = "0";
		cout << ans << endl;
	}
}