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 <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int w, h;
string s[21];

string solve()
{
    vector<string> result;
    int cnt = 0, wcnt = 0, hcnt = 0;
    int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
    int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            // ???????????????s[i][j]
            for(int k = 0; k < 8; k++){
                string tmp = "";
                int sx = i, sy = j;
                while(!(sx == i && sy == j) || tmp.length() == 0){
                    tmp += s[sx][sy];
                    // cout << tmp << endl;
                    sx += dx[k];
                    sy += dy[k];
                    if(sx >= h) sx = 0;
                    if(sy >= w) sy = 0;
                    if(sx < 0) sx = h - 1;
                    if(sy < 0) sy = w - 1;
                }
                result.push_back(tmp);
            }
        }
    }
    sort(result.begin(), result.end());
    string ans = "";
    for(int i = 0; i < result.size() - 1; i++){
        // cout << result[i] << endl;
        if(result[i].length() < ans.length()) continue;
        int cnt = 0;
        string nowans = "";
        while(cnt < min(result[i].length(), result[i + 1].length())){
            if(result[i][cnt] == result[i + 1][cnt]){
                nowans += result[i][cnt];
                cnt++;
            } else {
                break;
            }
        }
        if(nowans.length() > ans.length()) ans = nowans;
    }
    if(ans.length() < 2) return "0";
    else return ans;
}

int main()
{
    while(1){
        cin >> h >> w;
        if(h == 0 && w == 0) break;
        for(int i = 0; i < h; i++){
            cin >> s[i];
        }
        cout << solve() << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;

#define LET(name, value) __typeof(value) name = value
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (LET(i, (c).begin()); i != (c).end(); ++i)

char field[30][30];

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w), h|w) {
    REP(y, h) REP(x, w) { scanf(" %c", &field[y][x]); }

    static const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    static const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
    set<string> dict;
    string ans, str;
    REP(sy, h) REP(sx, w) REP(dir, 8) {
      int y = sy, x = sx;
      str.clear();
      do {
        str += field[y][x];
        LET(it, dict.lower_bound(str));
        if (it != dict.end() && *it == str) {
          if (str.size() > ans.size() || (str.size() == ans.size() && str < ans)) {
            ans = str;
          }
        } else {
          dict.insert(it, str);
        }
        y = (y + dy[dir] + h) % h;
        x = (x + dx[dir] + w) % w;
      } while (y != sy || x != sx);
    }
    printf("%s\n", ans.size() > 1 ? ans.c_str() : "0");
  }
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1, 0,-1, 1,-1, 1, 0,-1};
const int dy[] = {1, 1, 1, 0, 0,-1,-1,-1};


const double EPS = 1e-9;

int main(){
    while(true){
        int H,W;
        cin >> H >> W;
        if(H == 0) break;
        vector<string> donut(H);
        for(int i=0;i<H;i++){
            cin >> donut[i];
        }
        map<string,int> spells;
        for(int y=0;y<H;y++){
            for(int x=0;x<W;x++){
                for(int i=0;i<8;i++){
                    string spell = "";
                    int nx = x,ny = y;
                    set<pair<int,int> > alr;
                    while(alr.find(make_pair(ny,nx)) == alr.end()){
                        alr.insert(make_pair(ny,nx));
                        spell += donut[ny][nx];
                        if(spell.length() > 1)
                            spells[spell]++;
                        ny += dy[i];
                        nx += dx[i];
                        if(ny < 0) ny = H-1;
                        if(ny >= H) ny = 0;
                        if(nx < 0) nx = W-1;
                        if(nx >= W) nx = 0;
                    }
                }
            }
        }
        string maxi = "0";
        for(map<string,int>::iterator it=spells.begin();it!=spells.end();++it){
            if((*it).second > 1){
                if(maxi.length() == (*it).first.length()){
                    maxi = min(maxi,(*it).first);
                }else if(maxi.length() < (*it).first.length()){
                    maxi = (*it).first;
                }
            }
        }
        cout << maxi << endl;
    }
    return 0;
}