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):
    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: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    dd = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

    while True:
        h,w = LI()
        if h == 0 and w == 0:
            break

        a = [S() for _ in range(h)]

        rs = set()
        r = ''
        rm = -1

        for i in range(h):
            for j in range(w):
                for di,dj in dd:
                    tr = a[i][j]
                    for k in range(1,401):
                        ni = (i+di*k) % h
                        nj = (j+dj*k) % w
                        if ni == i and nj == j:
                            break
                        c = a[ni][nj]
                        tr += c
                        l = len(tr)
                        if tr in rs and (l > rm or (l==rm and r > tr)):
                            r = tr
                            rm = l
                        rs.add(tr)

        if r == '':
            rr.append(0)
        else:
            rr.append(r)


    return '\n'.join(map(str, rr))


print(main())