### constant

DXYS = [
  [ 1,  0], [ 1, -1], [ 0, -1], [-1, -1],
  [-1,  0], [-1,  1], [ 0,  1], [ 1,  1]
]

### main

loop do
  h, w = gets.split.map(&:to_i)
  break if (h | w) == 0

  dnts = h.times.map{gets.strip}
  #p dnts

  maxlen = 0
  spells = []

  for y0 in (0...h)
    for x0 in (0...w)
      DXYS.each do |dx, dy|
        x = x0
        y = y0
        str = dnts[y][x]
        len = 1
        loop do
          x = (x + dx) % w
          y = (y + dy) % h
          break if x == x0 && y == y0

          str += dnts[y][x]
          len += 1

          spells[len] = {} if spells[len].nil?
          if spells[len][str].nil?
            spells[len][str] = 0
          else
            spells[len][str] = 1
            maxlen = len if maxlen < len
          end
        end
      end
    end
  end
  #p maxlen
  #p spells

  if maxlen == 0
    puts 0
  else
    puts spells[maxlen].select{|k, v| v > 0}.keys.min
  end
end