while (h, w = gets.split.map(&:to_i)).inject(:+) != 0 do
  donut = Array.new
  count = Hash.new(0)
  h.times do
    donut.push(gets.chomp.chars)
  end
  h.times do |y|
    w.times do |x|
      dy = [0, 1, 1, 1, 0, -1, -1, -1]
      dx = [1, 1, 0, -1, -1, -1, 0, 1]
      8.times do |i|
        used = Array.new(h){Array.new(w,false)}
        string = donut[y][x]
        used[y][x] = true
        ny = (y + dy[i]) % h
        nx = (x + dx[i]) % w
        while !used[ny][nx] do
          string += donut[ny][nx]
          count[string] += 1
          used[ny][nx] = true
          ny = (ny + dy[i]) % h
          nx = (nx + dx[i]) % w
        end
      end
    end
  end
  spell = count.select{|key, value| value >= 2}.keys.sort{|v1, v2| v1.size <=> v2.size}
  spell.select!{|v| v.size == spell.last.size}
  puts spell.size >= 1 ? spell.sort.last : 0
end