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_i = self.lengths[0]
        max_j = self.lengths[1]
        max_diag = self.lengths[0] * self.lengths[1] / gcd_recursive(max_i, max_j)
        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 = 0
    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]

                # ignore shorter spells
                if direction[2] <= longest:
                    continue

                while length < longest:
                    spell += donut.get_letter([i, j, direction], length)
                    length += 1

                remaining_duplicates = []
                for duplicate in duplicates:
                    a = duplicate[0]
                    b = duplicate[1]
                    d = duplicate[2]
                    match = True
                    for l in range(0, length):
                        if donut.get_letter(duplicate, l) != spell[l]:
                            match = False
                            break

                    if match:
                        remaining_duplicates.append(duplicate)

                duplicates = remaining_duplicates

                # extend spell as long as there is a match
                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

                        if len(spell) > longest:
                            longest_spell = spell
                            longest = length
                    else:
                        break

                    if length >= direction[2]:
                        break

    return longest_spell


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())

    found_spell = compute(rows)
    if len(found_spell) == 1:
        print(0)
    else:
        print(found_spell)

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


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))