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 LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int H, W;
char field[10][20];

int ma;
string mas;

set<string> S;

void add_string(int i, int n, int m, int x, int y) {
    REP(j, n) {
        int sx = x, sy = y;
        stringstream ss;
        ss << field[sy][sx];
        REP(k, m) {
            sx = (sx + dx[i] + W) % W;
            sy = (sy + dy[i] + H) % H;
            ss << field[sy][sx];
            string s = ss.str();
            if (!S.insert(s).second) {
                if (ma < (int)s.length()) {
                    ma = s.length();
                    mas = s;
                } if (ma == (int)s.length()) {
                    mas = min(mas, s);
                }
            }
        }
        x = (x + dx[i] + W) % W;
        y = (y + dy[i] + H) % H;
    }
}

int main() {
    while (cin >> H >> W, H|W) {
        REP(y, H) REP(x, W) cin >> field[y][x];
        S.clear();

        ma = 0;
        REP(i, 8) {
            if (i < 4) {
                if (i % 2 == 0) {
                    REP(y, H) add_string(i, W, W-1, 0, y);
                } else {
                    REP(x, W) add_string(i, H, H-1, x, 0);
                }
            } else {
                REP(x, W) REP(y, H) {
                    add_string(i, 1, H*W-1, x, y);
                }
            }
        }
        if (ma == 0) {
            cout << 0 << endl;
        } else {
            cout << mas << endl;
        }
    }
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <vector>
#include <utility>
using namespace std;

set<string> Ws;
set<string> W;

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

typedef pair<int, string> spell;

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w) != EOF && w) {
    Ws.clear();
    vector< vector<char> > table = vector< vector<char> >(h, vector<char>(w));
    for (auto i=0; i<h; i++) {
      for (auto j=0; j<w; j++) {
	scanf(" %c", &table[i][j]);
      }
    }
    string MS = "";
    int MSsize = 0;
    spell A = make_pair(-1*MSsize, MS);
    for (auto i=0; i<h; i++) {
      for (auto j=0; j<w; j++) {
	W.clear();
	for (auto d=0; d<8; d++) {
	  string S = "";
	  S += table[i][j];
	  vector< vector<bool> > visited 
	    = vector< vector<bool> >(h, vector<bool>(w, false));
	  visited[i][j] = true;
	  int ni, nj;
	  for (auto k=1; 
	       !visited[(ni = (h*w+i+dx[d]*k)%h)][(nj = (h*w+j+dy[d]*k)%w)];
	       k++) {
	    visited[ni][nj] = true;
	    S += table[ni][nj];
	    // cerr << S << endl;
	    W.insert(S);
	  }
	}
	for (auto k=W.begin(); k != W.end(); k++) {
	  string S;
	  if (Ws.find((S = *k)) != Ws.end()) {
	    spell t;
	    if ((t = make_pair(-1*(int)(S.size()), S)) < A) A = t;
	  } else {
	    Ws.insert(S);
	  }
	}
      }
    }
    cout << (A.second == "" ? "0" : A.second) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int gcd(int a, int b){
    while(a != b){
        if(a > b) a = a - b; else b = b - a;
    }
    return a;
}

int lcm(int a, int b){
    return ((a / gcd(a, b)) * b);
}

int main(){
    int h, w;
    while(true){
char torus[10][20];
        string str[2048];
        bool ok[2048];
        int num = 0;

        for(int i=0; i<2048; ++i){
            str[i] = "";
            ok[i] = true;
        }

        cin >> h >> w;
        if(h==0 && w==0) break;

        int cml = lcm(w, h);

        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                cin >> torus[i][j];
            }
        }

        //一番長い文字列をつくる

        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<h; ++i){
                    str[num] += torus[(y+i)%h][x];
                    str[num+1] += torus[h-(y+i)%h-1][x];
                }
                num += 2;
            }
        }
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<w; ++i){
                    str[num] += torus[y][(x+i)%w];
                    str[num+1] += torus[y][w-(x+i)%w-1];
                }
                num += 2;
            }
        }
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int i=0; i<cml; ++i){
                    str[num] += torus[(y+i)%h][(x+i)%w];
                    str[num+1] += torus[(y+i)%h][w-(x+i)%w-1];
                    str[num+2] += torus[h-(y+i)%h-1][(x+i)%w];
                    str[num+3] += torus[h-(y+i)%h-1][w-(x+i)%w-1];
                }
                num += 4;
            }
        }


        string result = "";

        sort(str, str+num);

        for(int i=1; i<cml; ++i){
            for(int j=num-1; j>0; --j){
                if(ok[j] && str[j].length() >= i && str[j-1].length() >= i){
                    if(str[j].substr(0, i) == str[j-1].substr(0, i)){
                        result = str[j];
                        result.resize(i);
                    }else{
                        ok[j] = false;
                    }
                }
            }
        }

        cout << ((result.length() == 1) ? "0" : result) << endl;

    }

    return 0;
}