class Map
????attr_reader :data
????def initialize(ar_map)
????????@data = ar_map
????????@width = @data.count
????????@height = @data[0].count
????end
??
????def calc_islands
????????map_clone = Marshal.load(Marshal.dump(@data))
????????num_islands = 0
????????loop do
????????????next_pos = search_land(map_clone)
????????????if next_pos != false
????????????????fill_island(map_clone, next_pos[0], next_pos[1])
????????????????num_islands += 1
????????????else break
????????????end
????????end
????????num_islands
????end
??
????def search_land(map_data)
????????map_data.each_with_index do |ar_row, row|
????????????ar_row.each_with_index do |s, column|
????????????????return [row, column] if s == '1'
????????????end
????????end
????????false
????end
??
????def fill_island(map_data, x, y)
????????map_data[x][y] = '0'
????????ar_steps = [-1, 0, 1]
????????ar_steps.each do |x_p|
????????????ar_steps.each do |y_p|
????????????????next unless inside?(x + x_p, y + y_p)
????????????????if map_data[x + x_p][y + y_p] == '1'
????????????????????fill_island(map_data, x + x_p, y + y_p)
????????????????end
????????????end
????????end
????end
??
????def inside?(x, y)
????????(0..@width - 1).include?(x) && (0..@height - 1).include?(y)
????end
end
??
class MapManager
????attr_reader :ls_maps
????def initialize
????????@ls_maps = []
????end
??
????def load_input_maps(ar_input)
????????size_input = ar_input.count
????????num_head = 0
????????num_head = add_map(ar_input, num_head) while num_head < size_input - 1
????end
??
????def add_map(ar_input, num_head)
????????height_map = ar_input[num_head].split.map(&:to_i)[1]
????????ar_map_data = []
????????(num_head + 1).upto(num_head + height_map) do |i|
????????????ar_map_data << ar_input[i].split.map(&:to_s)
????????end
????????@ls_maps << Map.new(ar_map_data)
????????num_head += height_map + 1
????????num_head
????end
end
??
mm = MapManager.new
mm.load_input_maps(STDIN.readlines)
mm.ls_maps.each do |map|
????print map.calc_islands
end