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 <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

void init() {
    
}

const int dx[] = {0, 0, -1, -1, -1, 1, 1, 1};
const int dy[] = {1 ,-1, 1, 0, -1, 1, 0, -1};
const int MAX = 25;
char S[MAX][MAX];
map<string, int> cnt;

void solve(int h, int w) {
    for (int i = 0; i < h; i++) {
        scanf("%s", S[i]);
    }
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int d = 0; d < 8; d++) {
                string buf = "";
                set<pair<int, int>> se;
                //assert(buf == "");
                int ni = i, nj = j;
                while (true) {
                    if (se.count(make_pair(ni, nj))) break;
                    se.insert(make_pair(ni, nj));
                    buf += S[ni][nj];
                    if (buf.size() > 1) cnt[buf]++;
                    ni = (ni + dx[d] + h) % h;
                    nj = (nj + dy[d] + w) % w;
                }
                //DEBUG(buf)
                FORE(e, buf) assert(isupper(e));
            }
        }
    }
    string ans = "";
    for (auto e : cnt) {
        if (e.second > 1) {
            if (ans.length() <= e.first.length()) {
                if (ans.length() < e.first.length()) {
                    ans = e.first;
                } else if (ans > e.first) {
                    ans = e.first;
                }
            }
        }
    }
    if (ans == "") {
        ans = "0";
    }
    printf("%s\n", ans.c_str());
    cnt.clear();
}

signed main(void) {
    LL n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    init();
    while (cin >> n >> m, n) {
        solve(n, m);
    }
}

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

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

int h, w;
string s[10];

vector<string> generate(int sx, int sy, int dir)
{
	int x = sx, y = sy;
	string str = "";
	vector<string> res;
	
	while (true){
		str += s[x][y];
		res.push_back(str);
		x = (x + dx[dir] + h) % h;
		y = (y + dy[dir] + w) % w;
		if (sx == x && sy == y) break;
	}
	
	return res;
}

string solve()
{
	map<string, int> mp;
	
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			for (int d = 0; d < 8; d++){
				vector<string> vs = generate(i, j, d);
				for (auto &s : vs){
					mp[s]++;
				}
			}
		}
	}
	
	string res = "0";
	for (auto &p : mp){
		if (p.second < 2) continue;
		if (res.size() > p.first.size()) continue;
		if (res.size() < p.first.size()){
			res = p.first;
		}
		else res = min(res, p.first);
	}
	
	return res;
}

int main()
{
	while (cin >> h >> w, h){
		for (int i = 0; i < h; i++){
			cin >> s[i];
		}
		cout << solve() << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
//#include <unordered_set>
#include <set>

using namespace std;
char field[1001][1001];
bool used[1001][1001];

int main(){
  int H,W;
  while(cin>>H>>W&&(H|W)){
    for(int i=0;i<H;i++)
      for(int j=0;j<W;j++)
	cin>>field[i][j];
    vector<string> vs;
    for(int dy=-1;dy<=1;dy++){
      for(int dx=-1;dx<=1;dx++){
	if(dy==0&&dx==0)continue;
	memset(used,0,sizeof(used));
	for(int i=0;i<H;i++){
	  for(int j=0;j<W;j++){
	    if(!(i==0||j==0||i==H-1||j==W-1))continue;
	    int cx=j;
	    int cy=i;
	    string s;
	    if(used[cy][cx])continue;
	    while(1){
	      used[cy][cx]=true;
	      s+=field[cy][cx];
	      cy+=dy;cx+=dx;
	      cy=(cy+H)%H;
	      cx=(cx+W)%W;
	      if(used[cy][cx])break;
	    }
	    vs.push_back(s);
	    //cout<<s<<endl;
	  }
	}
      }
    }
    //unordered_set<string> us;
    set<string> us;
    string cand;
    for(int i=0;i<(int)vs.size();i++){
      for(int j=0;j<(int)vs[i].size();j++){
	for(int k=1;k<=(int)vs[i].size();k++){
	  string s;
	  for(int l=0;l<k;l++){
	    s+=vs[i][(j+l)%vs[i].size()];
	  }
	  if(us.count(s)==1){
	    if(cand==""||cand.size()<s.size())
	      cand=s;
	    else if(cand.size()==s.size()&&cand>s)
	      cand=s;
	  }
	  else if(us.count(s)==0)
	    us.insert(s);
	}
      }
    }
    if(cand.size()>1)cout<<cand<<endl;
    else cout<<0<<endl;
  }

  return 0;
}