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 rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

struct Trie{
  struct node{
    vector<node*> ch;
  public:
    int count;
    node(){
      ch=vector<node*>(26,NULL);
      count=0;
    }
  };
  node* root;
  string dfs(node* now){
    string s="";
    rep(i,26){
      if(now->ch[i]!=NULL){
        if(now->ch[i]->count>1){
          string t = string(1,'A'+i)+dfs(now->ch[i]);
          if(s.length()<t.length()){
            s=t;
          }
        }
      }
    }
    return s;
  }
  void edfs(node* now){
    rep(i,26){
      if(now->ch[i]!=NULL){
        edfs(now->ch[i]);
      }
    }
    delete now;
  }
public:
  Trie(){
    root = new node;
  }
  void add(string &s){
    node* now = root;
    int n = s.length();
    rep(i,n){
      if(now->ch[s[i]-'A']==NULL){
        now->ch[s[i]-'A'] = new node;
      }
      now=now->ch[s[i]-'A'];
      now->count++;
    }
  }
  string solve(){
    return dfs(root);
  }
  ~Trie(){
    edfs(root);
  }
};

int main(){
  vector<int> vx={-1,-1,-1,0,0,1,1,1};
  vector<int> vy={-1,0,1,-1,1,-1,0,1};
  while(1){
    int h,w;
    cin >> h >> w;
    if(h==0) return 0;
    vector<string> f(h);
    rep(i,h) cin >> f[i];
    Trie tr;
    rep(i,h){
      rep(j,w){
        rep(k,8){
          vector<vector<bool>> usd(h,vector<bool>(w,false));
          string s;
          int nx=i,ny=j;
          while(!usd[nx][ny]){
            usd[nx][ny]=true;
            s.push_back(f[nx][ny]);
            nx=(nx+vx[k]+h)%h,ny=(ny+vy[k]+w)%w;
          }
          tr.add(s);
        }
      }
    }
    string ans = tr.solve();
    if(ans.length()<=1) cout << 0 << endl;
    else cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[8]={-1,-1,-1,0,0,1,1,1},dy[8]={-1,0,1,-1,1,-1,0,1};
typedef pair<int,int> P;

void Main() {
  int n,m;
  while(cin >> n >> m && n) {
    string s[n];
    rep(i,n) R s[i];
    map<string,int> d;
    rep(i,n) {
      rep(j,m) {
        rep(k,8) {
          string t="";
          int x=i,y=j;
          while(1) {
            t+=s[x][y];
            d[t]++;
            x+=dx[k];
            y+=dy[k];
            if(x<0) x+=n;
            if(x>=n) x-=n;
            if(y<0) y+=m;
            if(y>=m) y-=m;
            if(x==i&&y==j) break;
          }
        }
      }
    }
    string ans="0";
    tr(it,d) {
      if(it->F.size()<=1 || it->S<2) continue;
      if(ans.size()<it->F.size()) ans=it->F;
      else if(ans.size()==it->F.size()) ans=min(ans,it->F);
    }
    pr(ans);
  }
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<set>
#include<unordered_set>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1}, dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int st2[15][25];
string s[15];
signed main(void)
{
	while(true) {
		int h, w;
		cin >> h >> w;
		if(!h && !w) break;
		REP(i, h) cin >> s[i];

		unordered_set<string> st;
		string ans = "";
		FOR(dist, 2, h*w+1) REP(i, h) REP(j, w) REP(vec, 8) {
			int y = i, x = j;
			string tmp = "";
			bool flag = true;
			//st2?????????
			REP(k, h) REP(l, w) st2[k][l] = 0;
			//????????????????????§?????????????????¢?????£????????????
			REP(k, dist) {
				tmp += s[y][x];
				//cout << y << " " << x << " " << s[y][x] << endl;
				x += dx[vec];
				y += dy[vec];
				if(x < 0) x = w-1;
				else if(x >= w) x = 0;
				if(y < 0) y = h-1;
				else if(y >= h) y = 0;
				if(st2[y][x] == 1) {
					flag = false;
					break;
				}
				st2[y][x] = 1;
			}
			//if(i == 3 && j == 5 && vec == 2) cout << tmp << endl;
			//st???????????¢????????????????????¨?????????ans??????????????´??°
			if(flag) {
				if(st.find(tmp) != st.end()) {
					if(tmp.size() > ans.size() || (tmp.size() == ans.size() && tmp < ans)) {
						ans = tmp;
						//cout << i << " " << j << " " << vec << " " << ans << endl;
					}
				}
				st.insert(tmp);
			}
		}
		if(ans == "") cout << 0 << endl;
		else cout << ans << endl;
	}

	return 0;
}