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 <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int H, W; string s[11];
int main() {
	while (cin >> H >> W, H | W) {
		for (int i = 0; i < H; i++) cin >> s[i];
		vector<string> res;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				for (int dy = -1; dy <= 1; dy++) {
					for (int dx = -1; dx <= 1; dx++) {
						if (dx == 0 && dy == 0) continue;
						string now;
						int tx = (j + dx + W) % W, ty = (i + dy + H) % H;
						now += s[ty][tx];
						while (tx != j || ty != i) {
							tx = (tx + dx + W) % W, ty = (ty + dy + H) % H;
							now += s[ty][tx];
							res.push_back(now);
						}
					}
				}
			}
		}
		sort(res.begin(), res.end());
		string ret;
		int pre = 0;
		for (int i = 1; i <= res.size(); i++) {
			if (i == res.size() || res[i - 1] != res[i]) {
				if (i - pre > 1) {
					if (ret.size() < res[pre].size() || (ret.size() == res[pre].size() && ret > res[pre])) {
						ret = res[pre];
					}
				}
				pre = i;
			}
		}
		if(ret.size() > 1) cout << ret << endl;
		else cout << 0 << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    while(1)
    {
        int nb_lignes,nb_colonnes;
        cin>>nb_lignes>>nb_colonnes;
        if(nb_lignes==0)
        {
            break;
        }
        vector<string> donut(nb_lignes);
        for(int c=0;c<donut.size();c++) cin>>donut[c];
        int dx[8]={1,1,1,0,0,-1,-1,-1};
        int dy[8]={0,1,-1,1,-1,0,1,-1};
        map<string,int> m;
        string best = "";
        for(int x=0;x<nb_lignes;x++)
        {
            for(int y=0;y<nb_colonnes;y++)
            {
                for(int d=0;d<8;d++)
                {
                    vector<vector<bool> > visites(nb_lignes,vector<bool>(nb_colonnes,false));
                    int xact = x;
                    int yact = y;
                    string act ="";
                    while(!visites[xact][yact])
                    {
                        act=act+donut[xact][yact];
                        m[act]++;
                        if((act.size()>best.size()||(act.size()==best.size()&&act<best))&&m[act]>1)
                            best=act;
                        visites[xact][yact]=true;
                        xact+=dx[d];
                        yact+=dy[d];
                        xact=(xact+nb_lignes)%nb_lignes;
                        yact=(yact+nb_colonnes)%nb_colonnes;
                    }
                }
            }
        }
        if(best.size()<2) cout<<0<<endl;
        else
            cout<<best<<endl;
    }
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>
using namespace std;

typedef long long ll;

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)

#define MAX_H (10+10)
#define MAX_W (20+10)

int h,w;
char t[MAX_H][MAX_W];
set<string> tmp;

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

void solve(){
  tmp.clear();
  string res = "";
  
  /*
  rep(i,h) rep(j,w){
    string s = "";
    s += t[i][j];
    cout << "res=" << res << endl;
    if(tmp.find(s) != tmp.end()){
      if(s.size() > res.size()) res = s;
      else if(s.size() == res.size() && s < res) res = s;
    }
    else{
      tmp.insert(s);
    }
  }
  */
  
  rep(i,h) rep(j,w) rep(k,8){
    int sx = j, sy = i;
    int x = j, y = i;
    string s = "";
    s += t[y][x];
    x += w+dx[k]; x %= w;
    y += h+dy[k]; y %= h;

    while(!(x == sx && y == sy)){
      s += t[y][x];
      x += w+dx[k]; x %= w;
      y += h+dy[k]; y %= h;

//      cout << "sx=" << sx << ",sy=" << sy << endl;
//      cout << "x=" << x << ",y=" << y << endl;
//      cout << "s=" << s << endl;

      
      if(tmp.find(s) != tmp.end()){
        if(s.size() > res.size()) res = s;
        else if(s.size() == res.size() && s < res) res = s;
      }
      else{
        tmp.insert(s);
      }
    }
  }

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

void solve2(string s){
  
}

int main(){
  while(true){
    cin >> h >> w;
    if(h == 0 && w == 0) break;
    rep(i,h) cin >> t[i];
    /*
    if(h == 1){
      s = t[0];
      solve2(s);
    }
    else if(w == 1){
      rep(i,h) s += t[0][i];
      solve2(s);
    }
    */
    solve();
  }
}

C++: #include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;

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

using namespace std;
typedef pair<int ,int > P;
typedef long long ll;

int H,W;
string donut[11];
map<string, int> res;

void dfs(string str,int sy,int sx, int y,int x,int a) {
    int ny = (y+dy[a]+H) % H;
    int nx = (x+dx[a]+W) % W;
    if(sy==ny && sx==nx) return;

    res[str]++;
    dfs(str+donut[ny][nx],sy,sx,ny,nx,a);
}

string cmp(string s1,string s2) {
    if(s1.size() > s2.size()) return  s1;
    else if(s1.size() < s2.size()) return s2;
    else return s1 < s2 ? s1 : s2;
}

void solve() {
    res.clear();
    rep(i,11) donut[i]="";
    rep(i,H) cin>>donut[i];
    rep(y,H) rep(x,W) {
        rep(a,8) {
            string tmp;
            tmp = donut[y][x];
            dfs(tmp,y,x,y,x,a);
        }
    }
    string ans;
    for(const auto &it : res){
        if(it.sc >= 2) {
            ans = cmp(ans,it.fr);
        }
    }
    if(ans.size() <= 2) ans = "0";
    cout<<ans<<endl;
}

int main() {
    while(cin>>H>>W) {
        if(H+W==0) break;
        solve();
    }
    return 0;
}