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 ll long long
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1316

int h, w;

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

set<string> Set;
int Maxlen = 0;
string ans;
vector<string> donut;
vector<vector<int>> usage;

void solve(int i, int j) {
	for (int k = 0; k < 8;k++) {
		usage.clear();
		usage.resize(h, vector<int>(w, 0));
		string ret = "";
		int y = i, x = j;
		while (usage[y][x] == 0) {
			ret += donut[y][x];

			if (Set.find(ret) != Set.end()) {
				if (ret.length() > Maxlen) {
					Maxlen = ret.length();
					ans = ret;
				}
				else if (ret.length() == Maxlen && ret < ans) {
					ans = ret;
				}
			}
			else {
				Set.insert(ret);
			}

			usage[y][x] = 1;
			y = (y + dy[k] + h) % h;
			x = (x + dx[k] + w) % w;


		}
	}
}

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	while (cin >> h >> w) {
		if (h == 0 && w == 0)break;
		Maxlen = 0;
		ans = "";
		donut.clear();
		donut.resize(h);
		Set.clear();
		for (int i = 0; i < h;i++) {
			cin >> donut[i];
		}

		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				solve(i, j);
			}
		}
		if (ans.length() < 2) cout << 0 << endl;
		else cout << ans << endl;
	}

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#define REP(i,a,b) for (int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

int H, W;
int dw[] = {1,1,0,-1,-1,-1,0,1};
int dh[] = {0,1,1,1,0,-1,-1,-1};

int main(void){
    while (cin >> H >> W && H) {
        vector<string> S(H,"");
        rep(i, H) cin >> S[i];
        string ans = "";
        set<string> appear;
        
        rep(h, H) rep(w, W) rep(i, 8) {
            string s = "";
            s += S[h][w];
            int iw = w, ih = h;
            w = (w+dw[i]+W)%W; h = (h+dh[i]+H)%H;
            while (!(w == iw && h == ih)) {
                s += S[h][w];
                if (appear.find(s) != appear.end()) {
                    if (s.length()  > ans.length()) ans = s;
                    if (s.length() == ans.length()) ans = min(ans, s);
                }
                appear.insert(s);
                w = (w+dw[i]+W)%W; h = (h+dh[i]+H)%H;
            }
        }
        cout << (ans.length() > 1 ? ans:"0") << endl;
    }
    return 0;
}

C++: #include <cassert>// c
#include <ctime>
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
#include <tuple>

using namespace std;
typedef long long ll;

#define ALL(c) (c).begin(),(c).end()
#define FOR(i,l,r) for(int i=(int)l;i<(int)r;++i)
#define REP(i,n) FOR(i,0,n)
#define FORr(i,l,r) for(int i=(int)r-1;i>=(int)l;--i)
#define REPr(i,n) FORr(i,0,n)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())
//debug
#define DUMP(x)  cerr << #x << " = " << (x)
#define LINE()    cerr<< " (L" << __LINE__ << ")"

template<typename T,typename U> T pmod(T x,U M){return (x%M+M)%M;}

typedef long long ll;

//output
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){REP(i,as.size()){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){REP(i,as.size()){if(i!=0)os<<endl; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){os<<p.first<<" "<<p.second;return os;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){os << get<0>(t);return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){os << get<0>(t)<<" "<<get<1>(t);return os;}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4,typename T5> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t);return os;}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t)<<" "<<get<6>(t);return os;}

int H,W;
vector<string> board;
vector<vector<set<string>>> ss;
int dx[8]={1,1,0,-1,-1,-1,0,1},dy[8]={0,1,1,1,0,-1,-1,-1};
vector<vector<bool>> passed;

string last;
void dfs(int sy,int sx,int y,int x,int dir,string s){
	if(passed[y][x]){last=s;return;}
	passed[y][x]=true;
	s+=board[y][x];
	ss[sy][sx].insert(s);
	dfs(sy,sx,pmod(y+dy[dir],H),pmod(x+dx[dir],W),dir,s);
	return ;
}


map<string,int> si;int C=0;

int main() {
	cout <<fixed<<setprecision(15);	
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true){
		cin >> H >> W;if(H==0)break;

		board=vector<string>(H);REP(y,H)cin >> board[y];
		si.clear();C=0;
		ss=vector<vector<set<string>>>(H,vector<set<string>>(W));
		passed=vector<vector<bool>>(H,vector<bool>(W));

		map<string,int> smap;
		REP(y,H)REP(x,W)REP(d,8){
			REP(_y,H)fill(ALL(passed[_y]),false);
			dfs(y,x,y,x,d,"");
		}
		REP(y,H)REP(x,W)EACH(it,ss[y][x])smap[*it]++;
		
		string res="";bool isF=true;
		EACH(it,smap)if(it->second>=2){
			const string& s=it->first;
			if(isF)res=s;
			if(res.size()<s.size())res=s;
			else if(res.size()==s.size() && res>s)res=s;
			isF=false;
		}
		if(res.size()<2) cout << 0 <<endl;
		else cout << res <<endl;
	}

}