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>
using namespace std;

int lcm(int a,int b){	//a,b < 20だしいいよね
		if (a < b){
			swap(a,b);
		}
		int times = a * b;
		vector<int> num;
		int gcd,lcm;
		while(1){
			int n;
			n = a % b;
			if(n == 0){	break;}
			a = b;
			b = n;
		}
		gcd = b;
		lcm = times / gcd;
	return lcm;
}

int main(){
	int h,w;
	while(1){
		cin >> h >> w ;
		if(h == 0 && w == 0){	break;}
		char donut[10][20];
		for(int i=0;i<10;i++){
			for(int j=0;j<20;j++){
				donut[i][j] = 0;
			}
		}
		string code[1600];
		bool much[1600];
		for(int i = 0; i < 1600; i++){
			code[i] = "";
			much[i] = true;
		}	//初期化…

		int l = lcm(w,h);	//ななめの最大数
		int a = 0;	//strの数を数えます

		for(int i = 0; i < h; i++){
			for(int j =0; j < w; j++){
				cin >> donut[i][j];
			}
		}	//input

		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				for(int i = 0; i < h; i++){	//縦列
					code[a].append(1,donut[(y + i)%h][x]);
					code[a+1].append(1,donut[h-(y + i)%h-1][x]);
				}
				a += 2;
				for(int j = 0; j < w; j++){	//横列
					code[a].append(1,donut[y][(x + j)%w]);
					code[a+1].append(1,donut[y][w-(x+j)%w-1]);//absつけなくても0になりません！
				}
				a += 2;
				for(int k = 0; k < l; k++){	//ななめ
					code[a].append(1,donut[(y + k)%h][(x + k)%w]);
					code[a+1].append(1,donut[(y + k)%h][(l + x - k)%w]);
					code[a+2].append(1,donut[(l + y - k)%h][(x + k)%w]);
					code[a+3].append(1,donut[(l + y - k)%h][(l + x - k)%w]);
				}
				a += 4;
			}
		}
		string mag = "";
		//a:strの数-1

		sort(code,code+a);//nとn+1を比べます

		for(int i = 0; i < a-1; i++){
			if(code[i][0] != code[i+1][0]){
				much[i+1] = false;
			}
		}
		for(int i = 1; i < l; i++){	//2文字目から
			for(int j = a-1; j > 0; j--){	//逆順
				if(much[j] == true && code[j].length() >= i+1 && code[j-1].length() >= i+1){//=を忘れていました
					//jのi+1文字目とj-1のi+1文字目を比べるよ
					if(code[j][i] != code[j-1][i]){
						much[j] = false;
					}else{
						mag = code[j];
						mag.resize(i+1);
					}
				}
			}
		}
		if(mag == ""){
			cout << "0" << endl;
		}else{
			cout << mag << endl;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <iostream>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

int div_row[8] = {-1,-1,-1,0,0,1,1,1},div_col[8] = {-1,0,1,-1,1,-1,0,1},H,W;


int main(){

	int work_row,work_col,max_length;
	char table[10][21];
	string ans;

	set<string> SPELL;

	while(true){
		cin >> H >> W;
		if(H == 0 && W == 0)break;

		SPELL.clear();
		ans = "0";

		for(int i = 0; i < H; i++){
			for(int k = 0; k < W; k++)cin >>table[i][k];
		}

		max_length = 2;

		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				for(int a = 0; a < 8; a++){

					string work;
					work += table[row][col];

					work_row = (row + div_row[a]+H)%H;
					work_col = (col + div_col[a]+W)%W;

					while(work_row != row || work_col != col){
						work += table[work_row][work_col];

						if(SPELL.find(work) != SPELL.end()){
							if(work.length() > max_length){
								max_length = work.length();
								ans = work;
							}else if(work.length() == max_length){
								if(work < ans){
									ans = work;
								}
							}
						}else{
							SPELL.insert(work);
						}
						work_row = (work_row + div_row[a]+H)%H;
						work_col = (work_col + div_col[a]+W)%W;
					}
				}
			}
		}

		cout << ans << endl;

	}

	return 0;
}

C++: #include <bits/stdc++.h>
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()

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};

void go(int y, int x, int dir, int sy, int sx, map<string, int>& mp) {
  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];
    map<string, int> mp;
    rep(i, H) rep(j, W) {
      rep(k, 8) {
        go(i, j, k, i, j, mp);//] ++;
      }
    }

    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;
}

C++: #include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
  //?????¶????????§?§£????????¨?????????
  int H, W;
  while(cin >> H >> W, H||W){
    string pattern[H];
    for(int i = 0; i < H; ++i){
      cin >> pattern[i];
    }
    int ng = H*W + 1, ok = 2;
    int dx[] = {1,0,-1,0,1,1,-1,-1}, dy[] = {0,1,0,-1,1,-1,1,-1};
    string ans = "0";
    while(ng - ok > 1){
      int m = (ng + ok)/2;
      map<string,int> M;
      for(int i = 0; i < H; ++i){
	for(int j = 0; j < W; ++j){
	  for(int k = 0; k < 8; ++k){
	    string str(m,'.');
	    str[0] = pattern[i][j];
	    bool f = true;
	    for(int l = 1; l < m; ++l){//??????????????????
	      int s = ((i + l*dx[k])%H+H)%H, t = ((j + l*dy[k])%W+W)%W;
	      if(s == i && t == j){
		f = false;
		break;
	      }else{
		str[l] = pattern[s][t];
		//str += pattern[s][t];
	      }
	    }
	    //cout << str << endl;
	    if(f){
	      M[str] += 1;
	    }
	  }
	}
      }
      bool g = false;
      for(map<string,int>::iterator itr = M.begin(); itr != M.end(); ++itr){
	if(itr->second > 1){
	  g = true;
	  ans = itr->first;
	}
      }
      
      if(g){
	ok = m;
      }else{
	ng = m;
      }

    }
    
    cout << ans << endl;
    
  }
  return 0;
}