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: 
from sys import stdin

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 = stdin.readline().split()
    num_rows = int(inp_size[0])

    if num_rows == 0:
        break;

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

    print(compute(rows))

Python: '''
7 0 1
6   2
5 4 3
'''
dx=[1,0,-1,0,1,-1,1,-1]
dy=[0,1,0,-1,1,1,-1,-1]

while True:
    h,w=map(int,input().split())
    if h==0:
        break
    field=[list(input()) for i in range(h)]
    allword=set()
    answord=set()
    for i in range(h):
        for j in range(w):
            s=field[i][j]
            if s in allword:
                answord.add((len(s),s))
            else:
                allword.add(s)
            for k in range(8):
                used=[[False for _ in range(w)]for _ in range(h)]
                s=field[i][j]
                used[i][j]=True
                while True:
                    i=(i+dy[k])%h
                    j=(j+dx[k])%w
                    if used[i][j]:
                        break
                    s+=field[i][j]
                    if s in allword:
                        answord.add((len(s),s))
                    else:
                        allword.add(s)
    print(sorted(answord,key=lambda x:(-x[0],x[1]))[0][1] if answord and sorted(answord,key=lambda x:(-x[0],x[1]))[0][0]>1 else 0 )

Python: 
from sys import stdin

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 = stdin.readline().split()
        num_rows = int(inp_size[0])

        if num_rows == 0:
            break;

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

        print(compute(rows))