def find_land(areas)
  (0...@h).each do |y|
    (0...@w).each do |x|
      return [x, y] if areas[y][x] == '1'
    end
  end
  nil
end

def incheck(x, y)
  0 <= x && x < @w && 0 <= y && y < @h
end

def bfs(areas, x, y)
  q = []
  q << [x, y]
  until q.empty?
    x, y = q.shift
    areas[y][x] = '0'
    (-1..1).each do |dy|
      (-1..1).each do |dx|
        next if dy == 0 && dx == 0
        xx = x + dx; yy = y + dy
        next unless incheck(xx, yy)
        q << [xx, yy] if areas[yy][xx] == '1'
      end
    end
  end
  areas
end

def solve(areas, depth)
  x, y = find_land(areas)
  return depth unless x
  solve(bfs(areas, x, y), depth + 1)
end

loop do
  @w, @h = gets.split.map(&:to_i)
  break if @w == 0 && @h == 0
  areas = @h.times.map { gets.chomp.split }
  puts solve(areas, 0)
end