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 <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int dx[] = {0, 0, 1, -1, 1, -1, -1, 1};
int h, w, sy, sx, max_size;
map<string, int> mp;
string table[15];
string str;
void solve(char ch)
{
	for(int i = 0; i < 8; i++)
	{
		int ny = sy;
		int nx = sx;
		str = "";
		while(1)
		{
			str += table[ny][nx];
			mp[str]++;
			if(mp[str] >= 2)
				max_size = max(max_size, (int)str.size());
			int tmp_y = ny + dy[i];
			int tmp_x = nx + dx[i];
			if(tmp_y < 0) tmp_y = h - 1;
			if(tmp_x < 0) tmp_x = w - 1;
			if(tmp_y >= h) tmp_y = 0;
			if(tmp_x >= w) tmp_x = 0;
			ny = tmp_y;
			nx = tmp_x;
			if(tmp_y == sy && tmp_x == sx)
				break;
		}
	}
}	
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(cin >> h >> w, h | w)
	{
		mp.clear();
		str = "";
		max_size = 2;
		for(int i = 0; i < h; i++)
		{
			cin >> table[i];
		}
		for(int i = 0; i < h; i++)
		{
			for(int j = 0; j < w; j++)
			{
				sy = i; sx = j;
				solve(table[i][j]);
			}
		}
		bool flag = false;
		for(auto itr = mp.begin(); itr != mp.end(); itr++)
		{
			if(itr->second >= 2 && itr->first.size() == max_size)
			{
				flag = true;
				cout << itr->first << endl;
				break;
			}
		}
		if(!flag)
			cout << 0 << endl;
	}
	return 0;
}

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

int W, H;
string donuts[10];
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1,  0,  1, -1,1, -1,0, 1};

string ansstr;
set<string> strs;

int flgs[100][100];

void dfs(int x, int y, int d, string cur){
	x = (x + W) % W;
	y = (y + H) % H;
	if(flgs[y][x]) return;
	string c2 = cur + donuts[y][x];
	if(strs.find(c2) == strs.end()){
		strs.insert(c2);
	}else{
		if(ansstr.size() < c2.size() || (ansstr.size() == c2.size() && c2 < ansstr)){
			ansstr = c2;
		}
	}
	flgs[y][x] = true;
	dfs(x + dx[d], y + dy[d], d, c2);
	flgs[y][x] = false;
}

int main(){
	while(cin >> H >> W, H){
		for(int i = 0; i < H; i++){
			cin >> donuts[i];
		}
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				flgs[i][j] = 0;
			}
		}
		ansstr = "";
		strs.clear();
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				for(int k = 0; k < 8; k++){
					dfs(j, i, k, "");
				}
			}
		}
		if(ansstr.size() < 2){
			cout << 0 << endl;
		}else{
			cout << ansstr << endl;
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int dx[8]={1,-1,0,0,1,1,-1,-1},dy[8]={0,0,1,-1,1,-1,1,-1};
void renew(string &x,string y){
	if(x.size()>y.size()) return;
	if(x.size()<y.size()){
		x=y;
		return;
	}
	if(x>y) x=y;
}
int main(){
	while(true){
		int H,W;
		cin>>H>>W;
		if(H==0) break;
		string s[10];
		rep(i,H) cin>>s[i];
		string ans;
		rep(ai,H) rep(aj,W) rep(ad,8){
			//show(ai);
			//show(aj);
			//show(ad);
			string sa;
			int ax=ai,ay=aj;
			do{
				sa+=s[ax][ay];
				ax+=dx[ad],ay+=dy[ad];
				if(ax==H) ax=0;
				if(ay==W) ay=0;
				if(ax==-1) ax=H-1;
				if(ay==-1) ay=W-1;
				//show(ax);
				//show(ay);
			}while(!(ax==ai&&ay==aj));
			//show(sa);
			rep(bi,H) rep(bj,W) rep(bd,8){
				if(s[ai][aj]!=s[bi][bj]) break;
				if(ai==bi&&aj==bj&&ad==bd) continue;
				//show(bi);
				//show(bj);
				//show(bd);
				int bx=bi,by=bj,cnt=0;
				do{
					if(cnt>=sa.size()||sa[cnt]!=s[bx][by]) break;
					cnt++;
					bx+=dx[bd],by+=dy[bd];
					if(bx==H) bx=0;
					if(by==W) by=0;
					if(bx==-1) bx=H-1;
					if(by==-1) by=W-1;
				}while(!(bx==bi&&by==bj));
				renew(ans,sa.substr(0,cnt));
			}
		}
		if(ans.size()<=1) puts("0");
		else cout<<ans<<endl;
	}
}

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for(int i = (int)(s); i< (int)(e); ++i)

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

map<string, int> mp;
vector<string> input;
int H, W;

typedef long long ll;
typedef pair<int, int> pii;

void f(int x, int y, set<pii>& st, string& s, const int dir) {
  //  cout << x << " " << y << " " << dir <<" " << s << endl;
  //  cout <<mp.size() << endl;
  //  cout <<s.size() << " " << st.size() << " " << mp.size() << endl;
  s += input[y][x];
  if(s.size() >= 2)   mp[s]++;
  pii p;
  p = make_pair(y, x);
  st.insert(p);
  (x += dx[dir] + W) %= W;
  (y += dy[dir] + H) %= H;
  assert(x >= 0 && x < W);
  assert(y >= 0 && y < H);
  p = make_pair(y, x);
  if(st.count(p)) {
    //    mp[s]++;
    return;
  }
  f(x, y, st, s, dir);
}

int main() {
  while(cin >> H >> W) {
    if(H == 0 && W == 0) break;
    mp.clear();
    input.resize(H);
    rep(i, H) cin >> input[i];
    rep(i, H) rep(j, W) {
      //mp[input[i].substr(j, 1)]++;
      rep(k, 8) {
	set<pii> st;
	string s;
	f(j, i, st, s, k);
      }
    }
    string s;
    for(map<string, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
      if(it->second == 1) continue;
      if(s.size() < it->first.size()) s = it->first;
      else {
	if(s.size() == it->first.size() && s > it->first) s = it->first;
      }
    }
    if(s.size() == 0) cout << 0 << endl;
    else cout << s << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1) {
    int h,w;
    cin>>h>>w;
    if(!h)break;
    vector<string> t(h);
    REP(i,h)cin>>t[i];
    vector<string> alls;
    REP(i,h)REP(j,w) {
      int di[]={1,1,1,0,-1,-1,-1,0};
      int dj[]={1,0,-1,-1,-1,0,1,1};
      REP(k,8) {
        string str;
        REP(l,h*w) {
          int ni=(i+di[k]*l+h*w)%h;
          int nj=(j+dj[k]*l+h*w)%w;
          if(l&&ni==i&&nj==j) break;
          str += t[ni][nj];
          alls.push_back(str);
        }
      }
    }
    sort(begin(alls),end(alls),[](const string& lhs, const string& rhs){
      if (lhs.size() > rhs.size()) return true;
      if (lhs.size() == rhs.size() && lhs < rhs) return true;
      return false;
    });
    int n=alls.size();
    string ans="0";
    REP(i,n-1) {
      if (alls[i].size() == 1) break;
      if(alls[i] == alls[i+1]) {
        ans = alls[i];
        break;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}