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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
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 = (1LL << 31) - 1;
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
#define MAX_N 100100 * 3

int h, w;
string s[100];
set<string> vs;

void make_s(int y, int x) {
	string res = "";
	int posx = x, posy = y;
	while (1) {
		posx++;
		posy++;
		if (posx == w)posx = 0;
		if (posy == h)posy = 0;
		res += s[posy][posx];
		if (posy == y&&posx == x)break;
	}
	vs.insert(res);
	//reverse(ALL(res));
	//vs.insert(res);

	res.clear();
	posx = x, posy = y;
	while (1) {
		posx--;
		posy++;
		if (posx == -1)posx = w - 1;
		if (posy == h)posy = 0;
		res += s[posy][posx];
		if (posy == y&&posx == x)break;
	}
	vs.insert(res);
	//reverse(ALL(res));
	//vs.insert(res);
}

int main() {
	while (cin >> h >> w&&w + h) {
		string ans = "";
		vs.clear();
		REP(i, 100)s[i].clear();
		REP(i, h)cin >> s[i];
		REP(i, h) {
			string t = s[i];
			vs.insert(t);
		}
		REP(i, w) {
			string t = "";
			REP(j, h)t += s[j][i];
			vs.insert(t);
		}
		REP(i, h) {
			make_s(i, 0);
			if (h != w)break;
		}
		for (auto it : vs) {
			bool F = 0;
			char c = it[0];
			REP(i, it.size()) {
				if (c != it[i])break;
				if (i == it.size() - 1)F = 1;
			}
			if (F) {
				if (ans.size() < it.size()) {
					ans = it;
				}
				else if (ans.size() == it.size()) {
					if (it < ans)ans = it;
				}
				continue;
			}
			string t = it + it;
			reverse(ALL(t));
			string r = t;
			reverse(ALL(t));
			//cout << t << endl;
			//cout << r << endl;
			for (int i = 0; i < it.size(); i++) {
				for (int j = 1; j <= it.size(); j++) {
					string buf = t.substr(i, j);
					bool f = 0;
					int now = t.size();
					while (t.find(buf) != t.rfind(buf, now)) {
						if (t.rfind(buf, now) - t.find(buf) < it.size())f = 1;
						now = t.rfind(buf, now) - 1;
						//cout << now << endl;
						if (now == -1)break;
					}
					if (r.find(buf) != -1)f = 1;
					if (f) {
						if (ans.size() < buf.size()) {
							ans = buf;
						}
						else if (ans.size() == buf.size()) {
							if (buf < ans)ans = buf;
						}
					}
					//buf = r.substr(i, j);
					//if (t.find(buf) != t.rfind(buf)) {
					//	if (ans.size() < buf.size()) {
					//		ans = buf;
					//	}
					//	else if (ans.size() == buf.size()) {
					//		if (buf < ans)ans = buf;
					//	}
					//}
				}
			}
		}
		if (ans.size() == 1)cout << 0 << endl;
		else cout << ans << endl;
	}
}

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

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

    for(;;){
        int h, w;
        cin >> h >> w;
        if(h == 0)
        return 0;

        vector<string> grid(h);
        for(int i=0; i<h; ++i)
        cin >> grid[i];

        set<string> ss;
        string ret;
        int maxLen = -1;
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                for(int d=0; d<8; ++d){
                    int y1 = y;
                    int x1 = x;
                    string s;
                    do{
                        s += grid[y1][x1];
                        y1 += dy[d] + h;
                        x1 += dx[d] + w;
                        y1 %= h;
                        x1 %= w;
                    }while(y1 != y || x1 != x);

                    int n = s.size();
                    while(n >= 2){
                        string t = s.substr(0, n);
                        if(ss.find(t) == ss.end()){
                            ss.insert(t);
                        }else{
                            if(n > maxLen || (n == maxLen && t < ret)){
                                maxLen = n;
                                ret = t;
                            }
                            break;
                        }
                        -- n;
                    }
                }
            }
        }

        if(maxLen == -1)
        cout << 0 << endl;
        else
        cout << ret << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
  int h,w;
  int i,j,k;
  int ai[]={ 1, 1, 1, 0, 0,-1,-1,-1};
  int aj[]={ 1, 0,-1, 1,-1, 1, 0,-1};
  cin >> h >> w;
  while(h!=0){
    string str[10];
    for(i=0;i<h;i++) cin >> str[i];
    vector <string> li;
    vector <string> ans;
    int mal=1;
    for(i=0;i<h;i++){
      //cout << i << endl;
      for(j=0;j<w;j++){
	for(k=0;k<8;k++){
	  int bi=i,bj=j;
	  string buf;
	  buf += str[i][j];
	  bi+=ai[k];bj+=aj[k];
	  if(bi<0) bi=h-1;
	  if(bi>=h) bi=0;
	  if(bj<0) bj=w-1;
	  if(bj>=w) bj=0;
	  while(bi!=i||bj!=j){
	    buf+=str[bi][bj];
	    if(buf.size()>=mal){
	      if(find(li.begin(),li.end(),buf)!=li.end()) {
		if(buf.size()>mal) ans.clear();
		ans.push_back(buf);
		mal=buf.size();
	      }else{
		li.push_back(buf);
	      }
	    }
	    bi+=ai[k];bj+=aj[k];
	    if(bi<0) bi=h-1;
	    if(bi>=h) bi=0;
	    if(bj<0) bj=w-1;
	    if(bj>=w) bj=0;
	  }
	}
      }
    }
    //for(i=0;i<li.size();i++) cout << li[i] << endl;
    sort(ans.begin(),ans.end());
    //for(i=0;i<ans.size();i++) cout << ans[i] << endl;
    if(ans.size()!=0) cout << ans[0] << endl;
    else cout << 0 << endl;
    cin >> h >> w;
  }

  return 0;
}