while true
    map = []
    until gets == nil || $_.chomp == ''
        map << $_.chomp.scan(/./)
    end

    def map.paint(i,j)
        unless (i<0 || i>=self.size) || (j<0 || j>=self[i].size)
            if self[i][j] == '1'
                self[i][j] = '0'
                self.paint(i-1,j)
                self.paint(i,j-1)
                self.paint(i+1,j)
                self.paint(i,j+1)
            end
        end
        nil
    end

    count = 0
    for i in [*0..map.size-1]
        for j in [*0..map[i].size-1]
            if map[i][j] == '1'
                count += 1
                map.paint(i,j)
            end
        end
    end
    puts count
    break if $_ == nil
end