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 <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=num-1; i>0; --i){
            int length = 0;
            string k = "";
            for(int j=0; j<min(str[i].length(), str[i-1].length()); ++j){
                if(str[i][j] == str[i-1][j]) k += str[i][j]; else break;
            }
            if(k.length() >= result.length()){
                result = k;
            }
        }

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

    }

    return 0;
}

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

using namespace std;

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

int main()
{
	long long int M , N;
	while(cin >> M >> N , M || N)
	{
		vector<vector<char>>D(M , vector<char>(N));
		set<string>SS;
		string ans;
		for(size_t i = 0; i < M; i++)
		{
			for(size_t j = 0; j < N; j++)
			{
				cin >> D[i][j];
			}
		}
		for(long long int i = 0; i < M; i++)
		{
			for(long long int j = 0; j < N; j++)
			{
				long long int startx = j , starty = i;
				for(long long int k = 0; k < 8; k++)
				{
					long long int p = 1;
					string s;
					s.push_back(D[starty][startx]);
					while(!( ( startx + p*dx[k] + N * 2000 ) % N == startx && ( starty + p*dy[k] + M * 2000 ) % M == starty ))
					{
						s.push_back(D[( starty + p*dy[k] + M * 205 ) % M][( startx + p*dx[k] + N * 205 ) % N]);
						if(s.length() > ans.length())
						{
							if(SS.find(s) != SS.end())
							{
								ans = s;
							} else
							{
								SS.insert(s);
							}
						}
						p++;
					}
				}
			}
		}
		if(ans.length<=1)
		{
			cout << 0 << endl;
		} else
		{
			cout << ans << endl;
		}
	}
}

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];
                        
                        set<string>::iterator 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++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE 

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

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

const int N = 20;
int visit[N][N];
int main(){
	int h, w;
	while (cin >> h >> w, h){
		vector<string> vs(h);
		rep(i, h) cin >> vs[i];

		map<string, int> m;
		rep(i, h) rep(j, w) rep(d, 8){
			MEMSET(visit, 0);
			string s;
			s += vs[i][j];
			visit[i][j] = 1;
			int nx = (j + dx[d] + w) % w, ny = (i + dy[d] + h) % h;
			while (!visit[ny][nx]){
				s += vs[ny][nx];
				++m[s];
				nx = (nx + dx[d] + w) % w, ny = (ny + dy[d] + h) % h;
			}
		}
		pair<int, string> ans = MP(0, "0");
		for (auto p : m){
			if (p.second > 1) ans = min(ans, MP(-(int)p.first.size(), p.first));
		}
		cout << ans.second << endl;
	}

	return 0;
}