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 "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

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

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int h, w;
    while (cin >> h >> w, h || w)
    {
        vector<string> v(h + 1);
        for (int i = 1; i <= h; i++)
        {
            cin >> v[i];
            v[i] = '$' + v[i];
        }
        vector<vector<P>> next(h + 2, vector<P>(w + 2));
        for (int i = 0; i <= h + 1; i++)
        {
            for (int j = 0; j <= w + 1; j++)
            {
                P p;
                if (i == 0)
                {
                    if (j == 0)
                        p = P(h, w);
                    else if (j == w + 1)
                        p = P(h, 1);
                    else
                        p = P(h, j);
                }
                else if (i == h + 1)
                {
                    if (j == 0)
                        p = P(1, w);
                    else if (j == w + 1)
                        p = P(1, 1);
                    else
                        p = P(1, j);
                }
                else if (j == 0)
                {
                    p = P(i, w);
                }
                else if (j == w + 1)
                {
                    p = P(i, 1);
                }
                else
                {
                    p = P(i, j);
                }
                next[i][j] = p;
            }
        }
        map<string, int> mp;
        vector<vector<bool>> used(h + 1, vector<bool>(w + 1, false));
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                for (int k = 0; k < 8; k++)
                {
                    for (int p = 1; p <= h; p++)
                        for (int q = 1; q <= w; q++)
                            used[p][q] = false;
                    string tmp = "";
                    int x = i, y = j;
                    while (!used[x][y])
                    {
                        used[x][y] = true;
                        tmp += v[x][y];
                        if(tmp.length()>=2) mp[tmp]++;
                        x += dx[k];
                        y += dy[k];
                        P p = next[x][y];
                        x = p.first;
                        y = p.second;
                    }
                }
            }
        }
        string res = "";
        for (auto v : mp)
        {
            if (v.second < 2)
                continue;
            if (v.first.length() < res.length())
                continue;
            if (res.length() == v.first.length())
                res = min(res, v.first);
            else
                res = v.first;
        }
        if (res.length() < 2)
            cout << 0 << endl;
        else
            cout << res << endl;
    }
}

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

int h, w;
char t[50][50];
bool used[50][50];
map<string,int> cnt;

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

void solve(int sx, int sy){
  for(int i = 0; i < 8; i++){
    int x = sx;
    int y = sy;
    string s = "";

    memset(used, 0, sizeof(used));

    while(!used[y][x]){
      used[y][x] = true;
      s += t[y][x];
      x = (x + dx[i] + w) % w;
      y = (y + dy[i] + h) % h;

      cnt[s]++;
      //string ts = s;
      //reverse(ts.begin(), ts.end());
      //cnt[ts]++;
    };
  }
}

int main(){
  while(cin >> h >> w, h || w){
    cnt.clear();

    for(int i = 0; i < h; i++){
      cin >> t[i];
    }

    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        solve(j, i);
      }
    }

    string ans = "";

    for(map<string,int>::iterator iter = cnt.begin(); iter != cnt.end(); iter++){
      string s = iter->first;
      int c = iter->second;

      if(2 <= c && 2 <= s.length() && ans.length() < s.length()){
        ans = s;
      }
    }

    if(ans == ""){
      cout << 0 << endl;
    }
    else{
      cout << ans << endl;
    }
  }
}