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>
#define var auto
using ll = long long;
const char newl = '\n';
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) {if(a > b) a = b;}
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) {if(a < b) a = b;}

using namespace std;

signed main(){
    while(true){
        int h, w;
        cin >> h >> w;
        if (h == 0) return 0;
        vector<string> s(h);
        for (int i = 0; i < h; i++) cin >> s[i];

        vector<pair<int, int>> dirs{
            make_pair(1, 1),
            make_pair(0, 1),
            make_pair(-1, 1),
            make_pair(1, 0),
            make_pair(-1, 0),
            make_pair(1, -1),
            make_pair(0, -1),
            make_pair(-1, -1),
        };

        string longest = "";

        unordered_set<string> se{};
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                for (auto&& dir : dirs){
                    int y = i, x = j;
                    string cur = "";
                    cur += s[y][x];
                    do{
                        y += dir.first;
                        x += dir.second;
                        if (y < 0) y += h;
                        if (x < 0) x += w;
                        if (h <= y) y -= h;
                        if (w <= x) x -= w;
                        if (y == i && x == j) break;
                        cur += s[y][x];
                        //cout << cur << endl;
                        if (!se.insert(cur).second){
                            if (cur.size() > longest.size() || (cur.size() == longest.size() && cur < longest)) longest = cur;
                        }
                    }
                    while (true);
                }
            }
        }


        cout << (longest.size() == 0 ? "0" : longest) << endl;
    }
}

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

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

int GCD(int a, int b) {
	return b ? GCD(b, a % b) : a;
}

int main()
{
	int h, w;
	while (cin >> h >> w, h | w) {
		int gcd = GCD(h, w);
		vector<string> c(h);
		for (int i = 0; i < h; i++) {
			cin >> c[i];
		}
		int ma = 0;
		set<string> exist;
		priority_queue<string, vector<string>, greater<string>> pq;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				for (int k = 0; k < 8; k++) {
					int x = i, y = j, lim = k & 1 ? h * w / gcd : k & 2 ? w : h;
					string s;
					s.push_back(c[x][y]);
					for (int l = 1; l < lim; l++) {
						x += dx[k]; y += dy[k];
						if (x >= h) {
							x -= h;
						}
						else if (x < 0) {
							x += h;
						}
						if (y >= w) {
							y -= w;
						}
						else if (y < 0) {
							y += w;
						}
						s.push_back(c[x][y]);
						if (exist.count(s)) {
							if ((int)s.size() > ma) {
								ma = s.size();
								pq = priority_queue<string, vector<string>, greater<string>>();
								pq.push(s);
							}
							else if ((int)s.size() == ma) {
								pq.push(s);
							}
						}
						exist.insert(s);
					}

				}
			}
		}
		if (ma) {
			cout << pq.top() << endl;
		}
		else {
			cout << 0 << endl;
		}
	}
	return 0;
}

C++: #include<string>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
char str[10][21];
int dx[]={1,1,1,0,0,-1,-1,-1};
int dy[]={1,0,-1,1,-1,1,0,-1};
vector<string> V;
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		V.clear();
		for(int i=0;i<a;i++)scanf("%s",str[i]);
		for(int i=0;i<a;i++){
			for(int j=0;j<b;j++){
				for(int k=0;k<8;k++){
					int row=i;
					int col=j;
					string S="";
					do{
						S+=str[row][col];
						row+=dx[k];
						col+=dy[k];
						if(row<0)row+=a;
						if(row>=a)row-=a;
						if(col<0)col+=b;
						if(col>=b)col-=b;
					}while(row!=i||col!=j);
					V.push_back(S);
				}
			}
		}
		std::sort(V.begin(),V.end());
		int len=0;
		int m=0;
		for(int i=0;i<V.size()-1;i++){
			int P=0;
			for(int j=0;j<min(V[i].size(),V[i+1].size());j++){
				if(V[i][j]!=V[i+1][j])break;
				P++;
			}
			if(P>len){
				len=P;
				m=i;
			}
		}
		if(len<2)printf("0\n");
		else{
			for(int i=0;i<len;i++)printf("%c",V[m][i]);
			printf("\n");
		}
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <map>
using namespace std;

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

int main() {
	int h, w;
	while(cin >> h >> w, h || w) {
		vector<string> in(h);
		rep(y,h) {
			cin >> in[y];
		}
		int dx[3] = {w-1, 0, 1};
		int dy[3] = {h-1, 0, 1};
		map<string, int> mp;
		rep(sy, h) {
			rep(sx, w) {
				rep(iy, 3) {
					rep(ix, 3) {
						if(ix==1 && iy==1) continue;
						int nx=sx, ny=sy;
						string str = "";
						do {
							str += in[ny][nx];
							nx = (nx + dx[ix]) % w;
							ny = (ny + dy[iy]) % h;
							mp[str]++;
						} while(nx!=sx || ny!=sy);
					}
				}
			}
		}
		int mx = 0;
		string res = "";
		for(map<string, int>::iterator it=mp.begin(); it!=mp.end(); it++) {
			if( it->second == 1 ) continue;
			if(res.length() < it->first.length() || (res.length()==it->first.length() && it->first < res)) {
				mx = it->second;
				res = it->first;
			}
		}
		if(res.length() < 2) {
			res = "0";
		}
		cout << res << endl;
	}
	return 0;
}