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<algorithm>
#include<vector>
#include<map>
#include<climits>
#include<iomanip>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int dx[] = {+0,+1,+0,-1,+1,+1,-1,-1};
int dy[] = {+1,+0,-1,+0,-1,+1,-1,+1};
int H,W;
map<string,int> cnt;
string ans;
vector<string> vec;

/*
void compareS(string &a,string b)
{
  int as = a.size();
  int bs = b.size();
  if(bs < 2)return;
  cnt[a]++,cnt[b]++;
  if(cnt[b] < 2)return;
  if(as > bs)return;
  if(as != bs)a = (as < bs)?b:a;
    a = min(a,b);
}
*/

void dfs(string *G,bool used[][20],int x,int y,int dir,string s)
{
  if(used[y][x])return;
  used[y][x] = true;
  //compareS(ans,s);
  if(s.size() >= 2)vec.push_back(s);
  int nx = (x+dx[dir]+W)%W, ny = (y+dy[dir]+H)%H;
  dfs(G,used,nx,ny,dir,s+G[ny][nx]);
}

int main()
{
  //clock_t s,t;
  //s = clock();
  while(cin >> H >> W,H|W)
    {
      vec.clear();
      cnt.clear();
      string G[H];
      rep(i,H)
	cin >> G[i];

      ans = "";
      rep(i,H)
	{
	  rep(j,W)
	    {
	      rep(k,8)
		{
		  bool used[10][20];
		  rep(a,H)rep(b,W)used[a][b] = false;		 
		  dfs(G,used,j,i,k,string(1,G[i][j]));
		}
	    }
	}
	  
      
      sort(vec.begin(),vec.end());
      rep(i,vec.size()-1)
	{
	  if(vec[i] == vec[i+1])
	    {
	      int vs = vec[i].size();
	      int as = ans.size();
	      if(vs < as)continue;
	      if(vs != as)ans = (as < vs)?vec[i]:ans;
	      else        ans = min(ans,vec[i]);
	      i++;
	    }
	}
      
	ans == ""?cout << 0 << endl:cout << ans << endl;

    }
  //t = clock();
  //cout << setiosflags(ios::fixed) << setprecision(10) << (double)(t-s)/CLOCKS_PER_SEC << endl;
  return 0;
}

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

void chmax(string& s1, string& s2) {
    if (s1.size() > s2.size()) return; 
    else if (s1.size() < s2.size()) {
        s1 = s2;
        return;
    }
    else {
        for (int i = 0; i < s1.size(); i++) {
            if (s1[i] > s2[i]) {
                s1 = s2;
                return;
            }
            else if (s1[i] < s2[i]) return;
        }
    }
}

bool is_palindrome(string s) {
    int n = s.size();
    for (int i = 0; i < n / 2; i++) {
        if (s[i] != s[n - i - 1]) return false;
    }
    return true;
}

int dh[3] = {-1, 0, 1}, dw[3] = {-1, 0, 1};

void solve(int h, int w) {
    vector<string> grid(h);
    for (int i = 0; i < h; i++) {
        cin >> grid[i];
    }
    vector<string> s;
    map<string, int> cnt;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int k = 0; k < 3; k++) {
                for (int l = 0; l < 3; l++) {
                    if (k == 1 && l == 1) continue;
                    int now_h = i, now_w = j;
                    string now_s;
                    do {
                        now_s.push_back(grid[now_h][now_w]);
                        s.push_back(now_s);
                        cnt[now_s]++;
                        now_h = (now_h + dh[k] + h) % h;
                        now_w = (now_w + dw[l] + w) % w;
                    } while (now_h != i || now_w != j);
                }
            }
        }
    }
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    string ans = "";
    for (int i = 0; i < s.size(); i++) {
        if (s[i].size() == 1) continue;
        if (cnt[s[i]] < 2 && !is_palindrome(s[i])) continue;
        chmax(ans, s[i]);
    }
    if (ans == "") cout << 0 << endl;
    else cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    while (true) {
        cin >> H >> W;
        if (H == 0 && W == 0) break;
        solve(H, W);
    }
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define ll long long
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1316

int h, w;

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

set<string> Set;
int Maxlen = 0;
string ans;
vector<vector<char>> donut;


void solve(int i, int j) {
	for (int k = 0; k < 8;k++) {
		vector<vector<int>> usage(h, vector<int>(w, 0));
		string ret = "";
		int y = i, x = j;
		while (usage[y][x] == 0) {
			ret += donut[y][x];

			if (Set.find(ret) != Set.end()) {
				if (ret.length() > Maxlen) {
					Maxlen = ret.length();
					ans = ret;
				}
				else if (ret.length() == Maxlen && ret < ans) {
					ans = ret;
				}
			}
			else {
				Set.insert(ret);
			}

			usage[y][x] = 1;
			y = (y + dy[k] + h) % h;
			x = (x + dx[k] + w) % w;


		}
	}
}

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	while (cin >> h >> w) {
		if (h == 0 && w == 0)break;
		Maxlen = 0;
		ans = "";
		donut.clear();
		donut.resize(h, vector<char>(w));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> donut[i][j];
			}
		}

		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				solve(i, j);
			}
		}
		if (ans.length() < 2) cout << 0 << endl;
		else cout << ans << endl;
	}

}

C++: #include <cstdio>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
#define REP(i,x)for(int i=0;i<x;i++)

string res;
int H,W,dx,dy,min_len;
char s[200];
char MAP[11][22];
bool visit[20][20];
vector<string> ss;
void dfs(int x,int y,int k){
  if(visit[y][x]){
    ss.push_back(string(s));
    return;
  }
  s[k]=MAP[y][x];  visit[y][x]=1;
  dfs((x + dx + W) % W, (y + dy + H) % H, k + 1);
  s[k]=0;  visit[y][x]=0;
}
int main() {
  while(scanf("%d%d",&W,&H),H){
    REP(i,H)scanf("%s",MAP[i]);
    ss.clear();
    min_len=1; res="0";
    REP(y,H)REP(x,W){
      for(dx=-1;dx<=1;dx++){
        for(dy=-1;dy<=1;dy++){
          if(dx|dy)dfs(x,y,0);
        }
      }
    }
    string res = "0";
    sort(ss.begin(),ss.end());
    REP(i,ss.size()-1){
      int len = 0;
      REP(k,min(ss[i].size(),ss[i+1].size())){
        if(ss[i][k]==ss[i+1][k])len++;
      }
      if(len < res.size())continue;
      if(res.size() == len){
        res = min(res, ss[i].substr(0,len));
      }else{
        res = ss[i].substr(0, len);
      }
    }
    puts(res.c_str());
  }
}