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;

int di[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dj[8] = {1, 1, 0, -1, -1, -1, 0, 1};


int main(){
    
    while(1){
        int h, w; cin >> h >> w;
        if(h == 0) break;
        vector<string> a(h);
        for(int i = 0; i < h; i++) cin >> a[i];
        string ans = "[";
        set<string> s;
        
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                for(int k = 0; k < 8; k++){

                    string tmp;
                    tmp += a[i][j];
                    int ni = (h + i + di[k]) % h;
                    int nj = (w + j + dj[k]) % w;
                    while(!(ni == i && nj == j)){
                        tmp += a[ni][nj];
                        //cout << i << " " << j << " " << ni << " " << nj << " " <<  tmp << endl;
                        
                        ni = (h + ni + di[k]) % h;
                        nj = (w + nj + dj[k]) % w;

                        //cout << i << " " << j << " " << ni << " " << nj << " " <<  tmp << endl;
                        if(s.find(tmp) != s.end()){
                            //cout << "find" << endl;
                            if(ans.size() < tmp.size()){
                                ans = tmp;
                            }else if(ans.size() == tmp.size() && ans > tmp){
                                ans = tmp;                                
                            }
                        }else{
                            //cout << "insert" << endl;
                            s.insert(tmp);
                        }                
                    }
                }
            }
        }

        //string hoge = "ABCDEFGHIJKLMNOPQRSTUVWXYZHHHHHABCDEFGHIJKLMNOPQRSTUVWXYZ";
        //cout << hoge.size() << endl;
    
        if(ans == "[") cout << 0 << endl;
        else cout << ans << endl;
    }

    return 0;
}

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

int di[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[] = {1, -1, 0, 0, 1, -1, 1, -1};
int main() {
    int h, w;
    while (cin >> h >> w, h) {
        vector<string> s(h);
        for (auto &si: s) cin >> si;
        set<string> se;
        vector<string> res;
        for (int k = 0; k < 8; k++) {
            for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
                string tmp;
                int ni = i, nj = j;
                do {
                    (ni += di[k] + h) %= h;
                    (nj += dj[k] + w) %= w;
                    tmp += s[ni][nj];
                    if (se.count(tmp)) res.emplace_back(tmp);
                    else se.emplace(tmp);
                } while (ni != i || nj != j);
            }
        }
        int len = 0;
        for (auto &si: res) len = max(len, (int)si.size());
        string ans = string(len, 'a');
        for (auto &si: res) if (si.size() == len) ans = min(ans, si);
        if (len < 2) cout << 0 << endl;
        else cout << ans << endl;
    }
    return 0;
}

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

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

bool solve() {
	int H, W; cin >> H >> W;
	if (H == 0 && W == 0) return (false);
	vector<string> S(H);
	for (int i = 0; i < H; i++) {
		cin >> S[i];
	}
	string ans;
	int length = 0;
	unordered_set<string> st;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			for (int k = 0; k < 8; k++) {
				bool flag[11][22] = {};
				int y = i, x = j;
				string s;	
				int size = 0;
				while (!flag[y][x]) {
					s += S[y][x];
					//cout << s << endl;
					size++;
					if (st.find(s) != st.end()) {
						if (length < size) {
							ans = s;
							length = size;
						} else if (length == size && ans > s) {
							ans = s;
						}
					}
					st.insert(s);
					flag[y][x] = true;
					(y += dy[k] + H) %= H, (x += dx[k] + W) %= W;
				}
			}
		}
	}
	if (ans.size() > 1) {
		cout << ans << endl;
	} else {
		cout << 0 << endl;
	}
	return (true);
}

int main() {
	while (solve());
}

C++: #include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;

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

using namespace std;
typedef pair<int ,int > P;
typedef long long ll;

int H,W;
string donut[11];
map<string, int> res;

void dfs(string str,int sy,int sx, int y,int x,int a) {
    int ny = (y+dy[a]+H) % H;
    int nx = (x+dx[a]+W) % W;
    res[str]++;
    if(sy==ny && sx==nx) return;

    dfs(str+donut[ny][nx],sy,sx,ny,nx,a);
}

string cmp(string s1,string s2) {
    if(s1.size() > s2.size()) return  s1;
    else if(s1.size() < s2.size()) return s2;
    else return s1 < s2 ? s1 : s2;
}

void solve() {
    res.clear();
    rep(i,11) donut[i]="";
    rep(i,H) cin>>donut[i];
    rep(y,H) rep(x,W) {
        rep(a,8) {
            string tmp;
            tmp = donut[y][x];
            dfs(tmp,y,x,y,x,a);
        }
    }
    string ans;
    for(const auto &it : res){
        if(it.sc >= 2) {
            ans = cmp(ans,it.fr);
        }
    }
    if(ans.size() <= 2) ans = "0";
    cout<<ans<<endl;
}

int main() {
    while(cin>>H>>W) {
        if(H+W==0) break;
        solve();
    }
    return 0;
}