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;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll h,w;

vector<string> vs;
map<string,ll> m;

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

void make(int y,int x){
    rep(i,8){
        string t = "";
        int ny = y,nx = x;
        while(1){
            t += vs[ny][nx];
            ny += dy[i];
            ny = (ny + h) % h;
            nx += dx[i];
            nx = (nx + w) % w;
            if(ny == y && nx == x)break;
        }
        m[t]++;
    }
}


int main(){
    while(cin >> h >> w&& h + w){
        vs.clear();
        vs.resize(h);
        m.clear();
        rep(i,h)cin >> vs[i];
        rep(i,h)rep(j,w)make(i,j);
        ll l = 0;
        string ans = "";
        for(auto it = m.begin();it != m.end();it++){
            if(it->second > 1){
                if(l < it->first.size()){
                    l = it->first.size();
                    ans = it->first;
                }
            }
        }
        map<string,ll>::iterator end = m.end();
        end--;
        for(auto it = m.begin();it != end;it++){
            auto nxt = it;
            nxt++;
            string s = it->first;
            string t = nxt->first;
            rep(i,min(t.size(),s.size())){
                if(s[i] != t[i]){
                    if(i != 0){
                        string buf = s.substr(0,i);
                        if(l < buf.size()){
                            // cout << s << "  " << t << endl;
                            // cout << "!!" << buf << endl;
                            l = buf.size();
                            ans = buf;
                        }
                    }
                    break;
                }
            }
        }
        if(l <= 1)cout << 0 << endl;
        else cout << ans << endl;
    }

}

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
		{
			auto ans2=ans;
			reverse(ans2.begin(),ans2.end());
			cout << (ans<ans2?ans:ans2) << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
int main(void)
{
  for(;;){
    int h,w;
    cin >> h >> w;
    if(!h) return 0;
    vector<string> f(h);
    REP(i,h) cin >> f[i];
    map<string,int> spell;
    int di[3]={h-1,0,1},dj[3]={w-1,0,1};
    REP(i,h) REP(j,w){
      REP(k,3) REP(l,3){
	string tmp="";
	int tmpi=i,tmpj=j;
	do{
	  tmp.push_back(f[tmpi][tmpj]);
	  ++spell[tmp];
	  tmpi=(tmpi+di[k])%h;
	  tmpj=(tmpj+dj[l])%w;
	}while(tmpi!=i||tmpj!=j);
      }
    }
    string answer="";
    for(auto x:spell){
      if(x.first.size()<=1 || x.second<2) continue;
      if(x.first.size()>answer.size()) answer=x.first;
      else if(x.first.size()==answer.size()) answer=min(answer,x.first);
    }
    if(answer.size()<2) cout << 0 << endl;
    else cout << answer << endl;
  }
}