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 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()) {
                    //cout << spell << endl;
                        ans = comp(ans, spell);
                    } else {
                        can.insert(spell);
                    }
                        x = (x + dx[k] + n) % n; y = (y + dy[k] + m) % m;
                    }
                    /*if (can.find(spell) != can.end()) {
                    //cout << spell << endl;
                        ans = comp(ans, spell);
                    } else {
                        can.insert(spell);
                    }*/
                }
            }
        }
        cout << ans << 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;
}

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

int lcm(int a,int b){	//a,b < 20だしいいよね
		if (a < b){
			swap(a,b);
		}
		int times = a * b;
		int gcd,lcm;
		while(1){
			int n;
			n = a % b;
			if(n == 0){	break;}
			a = b;
			b = n;
		}
		gcd = b;
		lcm = times / gcd;
	return lcm;
}

int main(){
	int h,w;
	while(1){
		cin >> h >> w ;
		if(h == 0 && w == 0){	break;}
		char donut[10][20];
		for(int i=0;i<10;i++){
			for(int j=0;j<20;j++){
				donut[i][j] = 0;
			}
		}
		string code[1600];
		bool much[1600];
		for(int i = 0; i < 1600; i++){
			code[i] = "";
			much[i] = true;
		}	//初期化…

		unsigned int l = lcm(w,h);	//ななめの最大数
		int a = 0;	//strの数を数えます

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

		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				for(int i = 0; i < h; i++){	//縦列
					code[a].append(1,donut[(y + i)%h][x]);
					code[a+1].append(1,donut[h-(y + i)%h-1][x]);
				}
				a += 2;
				for(int j = 0; j < w; j++){	//横列
					code[a].append(1,donut[y][(x + j)%w]);
					code[a+1].append(1,donut[y][w-(x+j)%w-1]);//absつけなくても0になりません！
				}
				a += 2;
				for(unsigned int k = 0; k < l; k++){	//ななめ
					code[a].append(1,donut[(y + k)%h][(x + k)%w]);
					code[a+1].append(1,donut[(y + k)%h][(l + x - k)%w]);
					code[a+2].append(1,donut[(l + y - k)%h][(x + k)%w]);
					code[a+3].append(1,donut[(l + y - k)%h][(l + x - k)%w]);
				}
				a += 4;
			}
		}
		string mag = "";	//a:strの数-1
		sort(code,code+a);//nとn+1を比べます

		for(unsigned int i = 1; i < l; i++){	//2文字目から
			for(int j = a-1; j > 0; j--){	//逆順
				if(much[j] == true && code[j].length() >= i+1 && code[j-1].length() >= i+1){//=を忘れていました
					//jのi+1文字目とj-1のi+1文字目を比べるよ
					if(code[j][i] != code[j-1][i]){
						much[j] = false;
					}else{
						mag = code[j];
						mag.resize(i+1);
					}
				}
			}
		}
		if(mag == ""){
			cout << "0" << endl;
		}else{
			cout << mag << endl;
		}
	}
	return 0;
}