import queue



def proceess(start_x,start_y):

    #print("start_x : " + str(start_x) + ", start_y : " + str(start_y))

    #insland = []

    #insland.append([start_x,start_y])

    new_island = queue.Queue()

    map[start_x][start_y] = 0

    new_island.put([start_x,start_y])

    while True:

        ob = new_island.get()

        x = ob[0]

        y = ob[1]

        #print("x : " + str(x) + ", y : " + str(y))



        for i in [-1,0,1]:

            for j in [-1,0,1]:

                nx = x + i

                ny = y + j



                if nx >= 0 and ny >= 0 and nx < h and ny < w:

                    if map[nx][ny] == 1:

                        #insland.append([nx,ny])

                        #print("ny : " + str(ny) + ", nx : " + str(nx))

                        new_island.put([nx,ny])

                        map[nx][ny] = 0



        #print("end 8 count.")

        if new_island.empty():

            break



while True:

    map = []



    tmp = input().split(" ")

    w = int(tmp[0])

    h = int(tmp[1])



    if w == 0 and h == 0:

        break





    for j in range(h):

        nova = []

        arm = eval(input())

        for k in arm.split(" "):

            #k.rstrip('\n')

            nova.append(int(k))

        map.append(nova)



    #print(map)



    count = 0

    for i in range(h):

        for j in range(w):

            if map[i][j] == 1:

                #print("i : " + str(i) + ", j : " + str(j))

                proceess(i,j)

                count += 1



    print(count)



import copy



move = ((1, 0), (0, 1), (0, -1), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1))





def dfs(start):

    field[start[1]][start[0]] = '0'

    CP = [start]

    while CP:

        NP = []

        for cp in CP:

            for m in move:

                n_p = (cp[0]+m[0], cp[1]+m[1])

                if not(n_p[0] < 0 or n_p[1] < 0 or n_p[1] >= h or n_p[0] >= w) and field[n_p[1]][n_p[0]] == '1':

                    field[n_p[1]][n_p[0]] = '0'

                    NP.append(n_p)

        CP = NP





while True:

    w, h = list(map(int, input().split()))

    if w == 0 and h == 0:

        break

    field = [list(input().split()) for _ in range(h)]

    count = 0

    for y in range(h):

        for x in range(w):

            if field[y][x] == '1':

                dfs((x, y))

                count += 1

    print(count)



import queue


def proceess(start_x, start_y):
    # print("start_x : " + str(start_x) + ", start_y : " + str(start_y))
    # insland = []
    # insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x, start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        # print("x : " + str(x) + ", y : " + str(y))
        for i in [-1, 0, 1]:
            for j in [-1, 0, 1]:
                nx = x + i
                ny = y + j
                if nx >= 0 and ny >= 0 and nx < h and ny < w:
                    if map[nx][ny] == 1:
                        # insland.append([nx,ny])
                        # print("ny : " + str(ny) + ", nx : " + str(nx))
                        new_island.put([nx, ny])
                        map[nx][ny] = 0
        # print("end 8 count.")
        if new_island.empty():
            break


while True:
    map = []
    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])
    if w == 0 and h == 0:
        break
    for j in range(h):
        nova = []
        arm = eval(input())
        for k in arm.split(" "):
            # k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)
    # print(map)
    count = 0
    for i in range(h):
        for j in range(w):
            if map[i][j] == 1:
                # print("i : " + str(i) + ", j : " + str(j))
                proceess(i, j)
                count += 1
    print(count)

import copy

move = ((1, 0), (0, 1), (0, -1), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1))


def dfs(start):
    field[start[1]][start[0]] = "0"
    CP = [start]
    while CP:
        NP = []
        for cp in CP:
            for m in move:
                n_p = (cp[0] + m[0], cp[1] + m[1])
                if (
                    not (n_p[0] < 0 or n_p[1] < 0 or n_p[1] >= h or n_p[0] >= w)
                    and field[n_p[1]][n_p[0]] == "1"
                ):
                    field[n_p[1]][n_p[0]] = "0"
                    NP.append(n_p)
        CP = NP


while True:
    w, h = list(map(int, input().split()))
    if w == 0 and h == 0:
        break
    field = [list(input().split()) for _ in range(h)]
    count = 0
    for y in range(h):
        for x in range(w):
            if field[y][x] == "1":
                dfs((x, y))
                count += 1
    print(count)

-import queue +import copy + +move = ((1, 0), (0, 1), (0, -1), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1)) -def proceess(start_x, start_y): -    # print("start_x : " + str(start_x) + ", start_y : " + str(start_y)) -    # insland = [] -    # insland.append([start_x,start_y]) -    new_island = queue.Queue() -    map[start_x][start_y] = 0 -    new_island.put([start_x, start_y]) -    while True: -        ob = new_island.get() -        x = ob[0] -        y = ob[1] -        # print("x : " + str(x) + ", y : " + str(y)) -        for i in [-1, 0, 1]: -            for j in [-1, 0, 1]: -                nx = x + i -                ny = y + j -                if nx >= 0 and ny >= 0 and nx < h and ny < w: -                    if map[nx][ny] == 1: -                        # insland.append([nx,ny]) -                        # print("ny : " + str(ny) + ", nx : " + str(nx)) -                        new_island.put([nx, ny]) -                        map[nx][ny] = 0 -        # print("end 8 count.") -        if new_island.empty(): -            break +def dfs(start): +    field[start[1]][start[0]] = "0" +    CP = [start] +    while CP: +        NP = [] +        for cp in CP: +            for m in move: +                n_p = (cp[0] + m[0], cp[1] + m[1]) +                if ( +                    not (n_p[0] < 0 or n_p[1] < 0 or n_p[1] >= h or n_p[0] >= w) +                    and field[n_p[1]][n_p[0]] == "1" +                ): +                    field[n_p[1]][n_p[0]] = "0" +                    NP.append(n_p) +        CP = NP -    map = [] -    tmp = input().split(" ") -    w = int(tmp[0]) -    h = int(tmp[1]) +    w, h = list(map(int, input().split())) -    for j in range(h): -        nova = [] -        arm = eval(input()) -        for k in arm.split(" "): -            # k.rstrip('\n') -            nova.append(int(k)) -        map.append(nova) -    # print(map) +    field = [list(input().split()) for _ in range(h)] -    for i in range(h): -        for j in range(w): -            if map[i][j] == 1: -                # print("i : " + str(i) + ", j : " + str(j)) -                proceess(i, j) +    for y in range(h): +        for x in range(w): +            if field[y][x] == "1": +                dfs((x, y))