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 <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

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

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

int H, W;
map<string, int> past;
char field[12][22];

void solve(int x, int y){
  rep(i, 8){
    bool used[12][22];
    string s = "";
    memset(used, 0, sizeof(used));
    used[y][x] = true;
    s += field[y][x];
    int nx = x+dx[i], ny = y+dy[i];
    if(nx < 0) nx = W-1;
    else if(nx >= W) nx = 0;
    if(ny < 0) ny = H-1;
    else if(ny >= H) ny = 0;
   while(true){
      if(used[ny][nx]) break;
      used[ny][nx] = true;
      s += field[ny][nx];
      nx += dx[i], ny += dy[i];
      if(nx < 0) nx = W-1;
      else if(nx >= W) nx = 0;
      if(ny < 0) ny = H-1;
      else if(ny >= H) ny = 0;
      if(s.size() >= 2) past[s]++;
      //if(s == "MF") cout << "x = " << x << ", y = " << y << endl;
    }
  }
}

void init(){
  past.clear();
}  

int main(){
  while(cin >> H >> W, H|W){
    init();
    rep(i, H){
      cin >> field[i];
    }
    rep(i, H){
      rep(j, W){
	solve(j, i);
      }
    }
    
    int L = 0;
    string ans = "";
    
    for(map<string, int>::iterator it = past.begin() ; it != past.end() ; it++){
      if(it->second < 2 ) continue;
      string tmp = it->first;
      //cout << tmp << endl;
      if(tmp.size() > L){
	L = tmp.size();
	ans = tmp;
      }
      else if(tmp.size() == L){
	if(ans > tmp){
	  ans = tmp;
	}
      }
    }
    if(ans.size() == 0) cout << "0" << endl;
    else cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);

//int dxy[5] = {0, 1, 0, -1, 0};
int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
// assign

string comp(string a, string b);

signed main()
{
    int n, m;
    while (cin >> n >> m, n) {
        vector<string> donut(n);
        rep(i, n) {
            cin >> donut[i];
        }
        set<string> can;
        string ans = "z";
        rep(i, n) {
            rep(j, m) {
                rep(k, 8) {
                    vvb used(n, vb(m, false));
                    string spell;
                    int x = i, y = j;
                    while (!used[x][y]) {
                        spell.pb(donut[x][y]);
                        used[x][y] = true;
                        if (can.find(spell) != can.end()) {
                            ans = comp(ans, spell);
                        } else {
                            can.insert(spell);
                        }
                        x = (x + dx[k] + n) % n; y = (y + dy[k] + m) % m;
                    }
                }
            }
        }
        cout << ((ans.length() > 1) ? ans : 0) << endl;
    }
    return 0;
}

string comp(string a, string b)
{
    if (a.length() < b.length()) {
        return b;
    } else if (a.length() > b.length()) {
        return a;
    } else {
        rep(i, a.length()) {
            if (a[i] < b[i]) {
                return a;
            } else if (a[i] > b[i]) {
                return b;
            }
        }
    }
    return a;
}