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;
vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};
vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
void spell(map<string, int> &mp, vector<vector<char>> &c, int y, int x, int d){
  int h = c.size();
  int w = c[0].size();
  string S;
  int y2 = y;
  int x2 = x;
  while (1){
    S += c[y2][x2];
    mp[S]++;
    y2 = (y2 + h + dy[d]) % h;
    x2 = (x2 + w + dx[d]) % w;
    if (y2 == y && x2 == x){
      break;
    }
  }
}
int main(){
  while (1){
    int h, w;
    cin >> h >> w;
    if (h == 0 && w == 0){
      break;
    }
    vector<vector<char>> c(h, vector<char>(w));
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        cin >> c[i][j];
      }
    }
    map<string, int> mp;
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        for (int k = 0; k < 8; k++){
          spell(mp, c, i, j, k);
        }
      }
    }
    string ans = "A";
    for (auto P : mp){
      if (P.second >= 2){
        if (P.first.size() > ans.size() || P.first.size() == ans.size() && P.first < ans){
          ans = P.first;
        }
      }
    }
    if (ans == "A"){
      cout << 0 << endl;
    } else {
      cout << ans << endl;
    }
  }
}

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


int main() {
    int H, W;
    while (cin >> H >> W, H) {
        string field[10];
        for (int i = 0; i < H; i++) {
            cin >> field[i];
        }

        vector<string> cand;
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= +1; dy++) {
                        if (dx == 0 && dy == 0) continue;
                        bool visited[15][25] = { 0 };
                        int nx = x, ny = y;
                        string spell;
                        while (!visited[ny][nx]) {
                            visited[ny][nx] = true;
                            spell += field[ny][nx];
                            cand.push_back(spell);
                            nx = (nx + dx + W) % W;
                            ny = (ny + dy + H) % H;
                        }
                    }
                }
            }
        }
        
        string ans = "";
        sort(cand.begin(), cand.end());
        for (int i = 1; i < (int)cand.size(); ++i) {
            if (cand[i].size() == 1) continue;
            if (cand[i] != cand[i - 1]) continue;
            if (ans.size() < cand[i].size() || ans > cand[i]) {
                ans = cand[i];
            }
        }
        cout << ans << endl;
    }
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>
#include <regex>

using namespace std;

#define C_MAX(a, b) ((a)>(b)?(a):(b))
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

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

bool comp(string &a, string &b) {
    if (a.size() < b.size()) return true;
    if (a.size() > b.size()) return false;
    return a > b;
}

int main() {

    while (true) {
        int H, W;
        cin >> H >> W;

        if (!(H | W)) break;

        vector<string> M(H);
        for (int i = 0; i < H; i++) cin >> M[i];

        map<string, int> MAP;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                for (int k = 0; k < 8; k++) {
                    string s = "";
                    vector<vector<bool>> B(H, vector<bool>(W, false));
                    int y = i, x = j;
                    while (true) {
                        if (B[y][x]) break;
                        s += M[y][x];
                        MAP[s] += 1;
                        B[y][x] = true;
                        y += (2 * H + dy[k]);
                        y %= H;
                        x += (2 * W + dx[k]);
                        x %= W;
                    }
                }
            }
        }

        vector<string> cand;
        for (auto x: MAP) if (x.second >= 2) cand.push_back(x.first);

        sort(cand.begin(), cand.end(), comp);
        if (cand.back().size() <= 1) cout << 0 << endl;
        else cout << cand.back() << endl;

    }

}

C++: #include<bits/stdc++.h>
using namespace std;
const int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};

int W, H;
string S[10];
map< string, int > sss;

void dfs(string T, int y, int x, const int sy, const int sx, const int dir)
{
  if(!T.empty() && y == sy && x == sx) return;
  sss[T + S[y][x]]++;
  dfs(T + S[y][x], (y + dy[dir] + H) % H, (x + dx[dir] + W) % W, sy, sx, dir);
}

bool compare(const string& x, const string& y)
{
  if(x.size() != y.size()) return(x.size() > y.size());
  return(x < y);
}

int main()
{
  while(cin >> H >> W, H) {
    for(int i = 0; i < H; i++) {
      cin >> S[i];
    }
    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        for(int k = 0; k < 8; k++) dfs("", i, j, i, j, k);
      }
    }
    string S = "";
    for(auto it : sss) {
      if(it.second >= 2) S = min(S, it.first, compare);
    }
    if(S.size() == 1) cout << 0 << endl;
    else cout << S << endl;
    sss.clear();
  }
}