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 <stdio.h>
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,m,n) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define VEC2(x,a,b) vector<vector<ll>> x = vector<vector<ll>>(a,vector<ll>(b,0))
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cinv(v,n,m) FOR(i,n,m) cin>>v.at(i)
#define coutv(v,n,m) FOR(i,n,m) cout<<v.at(i) <<" "; cout<<endl
#define cout(n) cout<<fixed<<setprecision(n)

vector<string> v;
vector<string> list_ans;
ll dir_j[8] = {0,1,1,1,0,-1,-1,-1};
ll dir_i[8] = {1,1,0,-1,-1,-1,0,1};

void push(ll i, ll j){
  ll h = v.size();
  ll w = v[0].size();
  FOR(p,0,8){
    ll tmp_i = i;
    ll tmp_j = j;
    string s;
    s.push_back(v[tmp_i][tmp_j]);
    tmp_i += dir_i[p];
    tmp_j += dir_j[p];
    tmp_i += h;
    tmp_j += w;
    tmp_i %= h;
    tmp_j %= w;
    while(!(tmp_i == i && tmp_j == j)){
//      cout<<"t_i = "<<tmp_i<<" t_j = "<<tmp_j<<endl;
      s.push_back(v[tmp_i][tmp_j]);
      tmp_i += dir_i[p];
      tmp_j += dir_j[p];
      tmp_i += h;
      tmp_j += w;
      tmp_i %= h;
      tmp_j %= w;
    }
    list_ans.push_back(s);
  }
}

string ch_res(string a, string b){
  if(a.size()<b.size()){
    return b;
  }
  return a;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h,w;
    cin>>h>>w;
    while(h!=0){
      v = vector<string>(0,"");
      list_ans = vector<string>(0,"");
      FOR(i,0,h){
        string s;
        cin>>s;
        v.push_back(s);
      }

      FOR(i,0,h){
        FOR(j,0,w){
//          cout<<"i = "<<i<<" j = "<<j<<endl;
          push(i,j);
        }
      }

      sortAl(list_ans);
      string res;
      FOR(i,1,list_ans.size()){
        string a = list_ans[i-1];
        string b = list_ans[i];
        string tmp_a;
        string tmp_b;
        FOR(j,0,a.size()){
          tmp_a.push_back(a[j]);
          tmp_b.push_back(b[j]);
          if(tmp_a == tmp_b) res = ch_res(res, tmp_a);
        }
      }

      if(res.size() == 1) cout<<0<<endl;
      else cout<<res<<endl;

      cin>>h>>w;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
//#include <unordered_set>
#include <set>
 
using namespace std;
char field[1001][1001];
bool used[1001][1001];
 
int main(){
  int H,W;
  while(cin>>H>>W&&(H|W)){
    for(int i=0;i<H;i++)
      for(int j=0;j<W;j++)
    cin>>field[i][j];
    vector<string> vs;
    for(int dy=-1;dy<=1;dy++){
      for(int dx=-1;dx<=1;dx++){
    if(dy==0&&dx==0)continue;
    memset(used,0,sizeof(used));
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(!(i==0||j==0||i==H-1||j==W-1))continue;
        int cx=j;
        int cy=i;
        string s;
        if(used[cy][cx])continue;
        while(1){
          used[cy][cx]=true;
          s+=field[cy][cx];
          cy+=dy;cx+=dx;
          cy=(cy+H)%H;
          cx=(cx+W)%W;
          if(used[cy][cx])break;
        }
        vs.push_back(s);
        //cout<<s<<endl;
      }
    }
      }
    }
    //unordered_set<string> us;
    set<string> us;
    string cand;
    for(int i=0;i<(int)vs.size();i++){
      for(int j=0;j<(int)vs[i].size();j++){
    for(int k=1;k<=(int)vs[i].size();k++){
      string s;
      for(int l=0;l<k;l++){
        s+=vs[i][(j+l)%vs[i].size()];
      }
      if(us.count(s)==1){
        if(cand==""||cand.size()<s.size())
          cand=s;
        else if(cand.size()==s.size()&&cand>s)
          cand=s;
      }
      else if(us.count(s)==0)
        us.insert(s);
    }
      }
    }
    if(cand.size()>1)cout<<cand<<endl;
    else cout<<0<<endl;
  }
 
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

int H, W;
vector<string> F;

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

unordered_map<string, int> mp;

void go(int y, int x, int dir, int sy, int sx) {
  string s;
  while(1) {
    s += F[y][x];
    int ny = y + dy[dir];
    int nx = x + dx[dir];
    if(ny >= H) { ny = 0; }
    if(ny < 0) { ny = H-1; }
    if(nx >= W) { nx = 0; }
    if(nx < 0) { nx = W-1; }
    if(ny == sy && nx == sx) { break; }
    y = ny, x = nx;
    mp[s]++;
  }
}

int main() {

  for(; cin >> H >> W && (H | W);) {

    F.clear(); rep(i, H) F.resize(H);
    vector<string> v;
    rep(i, H) cin >> F[i];
    mp.clear();
    rep(i, H) rep(j, W) {
      rep(k, 8) {
        go(i, j, k, i, j);
      }
    }

    vector<string> u;
    for(auto && e: mp) {
      if(e.first.size() > 1 && e.second > 1) {
        u.push_back(e.first);
      }
    }

    if(u.empty()) {
      cout << 0 << endl;
    }
    else {
      sort(all(u), [&](string const& s, string const& t){ int n=s.size(),m=t.size(); return n != m ? n > m : s < t; });
      cout << u[0] << endl;
    }
  }
  
  return 0;
}