class DictNode
	def initialize(parent = nil, character = nil)
		@length = (parent ? parent.length + 1 : 0)
		@parent = parent
		@character = character
		@children = {}
	end

	def child(c)
		@children[c]
	end

	def add_child(c)
		@children[c] = self.class.new(self, c)
	end

	def to_s
		return "" if !@parent
		@parent.to_s + @character
	end

	attr_reader :length
end

loop do
	h, w = gets.split.map(&:to_i)
	break if w == 0
	donut = (1..h).map { gets.chomp.split("") }
	dict = DictNode.new
	spell = ''
	len = 0

	(0..h-1).each do |i0|
		(0..w-1).each do |j0|
			[-1, 0, 1].repeated_permutation(2) do |di, dj|
				next if di == dj && di == 0
				u = dict
				i, j = i0, j0
				loop do
					c = donut[i][j]
					if v = u.child(c)
						if v.length > len
							spell = v.to_s
							len = v.length
						elsif v.length == len
							s = v.to_s
							if s < spell
								spell = s
								len = v.length
							end
						end
					else
						u.add_child(c)
					end
					u = u.child(c)
					i = (i + di) % h
					j = (j + dj) % w
					break if i == i0 && j == j0
				end
			end
		end
	end
	puts spell.size > 1 ? spell : 0
end