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

Python: 
def gcd_recursive(a, b):
    if b == 0:
        return a
    else:
        return gcd_recursive(b, a % b)


def modulo(x, base):
    return (x % base + base) % base


class Donut:
    def __init__(self, letters):
        self.letters = letters
        self.lengths = [len(letters), len(letters[0])]

        max_diag = self.lengths[0] * self.lengths[1]
        max_i = self.lengths[0]
        max_j = self.lengths[1]
        self.directions = [
            [-1, -1, max_diag],
            [-1, 0, max_i],
            [-1, 1, max_diag],
            [0, -1, max_j],
            [0, 1, max_j],
            [1, -1, max_diag],
            [1, 0, max_i],
            [1, 1, max_diag]]

    def get_letter(self, start, dist):
        mi = modulo(start[0] + start[2][0]*dist, self.lengths[0])
        mj = modulo(start[1] + start[2][1]*dist, self.lengths[1])
        return self.letters[mi][mj]


def compute(rows):
    print('computing', rows)
    donut = Donut(rows)

    longest_spell = ''
    for i in range(0, donut.lengths[0]):
        for j in range(0, donut.lengths[1]):
            for direction in donut.directions:
                length = 0
                spell = ''
                duplicates = [[a, b, d]
                              for a in range(donut.lengths[0])
                              for b in range(donut.lengths[1])
                              for d in donut.directions]



                while True:
                    letter = donut.get_letter([i, j, direction], length)
                    remaining_duplicates = []
                    for duplicate in duplicates:
                        a = duplicate[0]
                        b = duplicate[1]
                        d = duplicate[2]
                        if donut.get_letter(duplicate, length) == letter:
                            remaining_duplicates.append(duplicate)

                    if len(remaining_duplicates) > 1:
                        length += 1
                        spell += letter
                        duplicates = remaining_duplicates
                    else:
                        break

                    if length >= direction[2]:
                        break

                if len(spell) > len(longest_spell):
                    longest_spell = spell

    return longest_spell


if __name__ == '__main__':
    while True:
        inp_size = input().split()
        num_rows = int(inp_size[0])

        if num_rows == 0:
            break;

        rows = []
        for i in range(int(num_rows)):
            rows.append(input())

        print(compute(rows))

Python: 
def gcd_recursive(a, b):
    if b == 0:
        return a
    else:
        return gcd_recursive(b, a % b)


def modulo(x, base):
    return (x % base + base) % base


class Donut:
    def __init__(self, letters):
        self.letters = letters
        self.lengths = [len(letters), len(letters[0])]

        max_diag = self.lengths[0] * self.lengths[1]
        max_i = self.lengths[0]
        max_j = self.lengths[1]
        self.directions = [
            [-1, -1, max_diag],
            [-1, 0, max_i],
            [-1, 1, max_diag],
            [0, -1, max_j],
            [0, 1, max_j],
            [1, -1, max_diag],
            [1, 0, max_i],
            [1, 1, max_diag]]

    def get_letter(self, start, dist):
        mi = modulo(start[0] + start[2][0]*dist, self.lengths[0])
        mj = modulo(start[1] + start[2][1]*dist, self.lengths[1])
        return self.letters[mi][mj]


def compute(rows):
    donut = Donut(rows)

    longest_spell = ''
    for i in range(0, donut.lengths[0]):
        for j in range(0, donut.lengths[1]):
            for direction in donut.directions:
                length = 0
                spell = ''
                duplicates = [[a, b, d]
                              for a in range(donut.lengths[0])
                              for b in range(donut.lengths[1])
                              for d in donut.directions]



                while True:
                    letter = donut.get_letter([i, j, direction], length)
                    remaining_duplicates = []
                    for duplicate in duplicates:
                        a = duplicate[0]
                        b = duplicate[1]
                        d = duplicate[2]
                        if donut.get_letter(duplicate, length) == letter:
                            remaining_duplicates.append(duplicate)

                    if len(remaining_duplicates) > 1:
                        length += 1
                        spell += letter
                        duplicates = remaining_duplicates
                    else:
                        break

                    if length >= direction[2]:
                        break

                if len(spell) > len(longest_spell):
                    longest_spell = spell

    return longest_spell


if __name__ == '__main__':
    while True:
        inp_size = input().split()
        num_rows = int(inp_size[0])

        if num_rows == 0:
            break;

        rows = []
        for i in range(int(num_rows)):
            rows.append(input())

        print(compute(rows))

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

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

typedef map<string,int>M;
M spell;
string mat[10];

void Search(int sx,int sy, int a, int h, int w)
{
	bool f[10][20]={};
	string str;
	int x,y;
	for(x=sx, y=sy; ; x=(x+dx[a]+w)%w, y=(y+dy[a]+h)%h)
	{
		if(f[y][x])
			break;
		f[y][x]=true;
		str+=mat[y][x];
		++spell[str];
	}
}

int main()
{
	int h,w;
	while(cin>>h>>w,h)
	{
		for(int i=0; i<h; ++i)
			cin >> mat[i];

		spell.clear();
		for(int x=0; x<w; ++x)
			for(int y=0; y<h; ++y)
				for(int a=0; a<8; ++a)
					Search(x,y,a,h,w);

		string res("");
		for(M::iterator ite=spell.begin(); ite!=spell.end(); ++ite)
		{
			if(ite->second<=1)
				continue;
			if(res.size()<ite->first.size() || res>ite->first)
				res=ite->first;
		}
		if(res.size()<=1)
			res="0";
		cout << res << endl;
	}
}