import sys



def visit(x, y, area):

    area[y][x] = 0

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

    #print '(' + str(x) + ',' + str(y) + ')'



    for i in move:

        if 0 <= (x + i[0]) < w and  0 <= (y + i[1]) < h and area[y + i[1]][x + i[0]] == 1:

            visit(x + i[0], y + i[1], area)



def solve():

    area = []

    ans = 0

    for i in range(h):

        area.append(list(map(int, input().split())))



    #print 'w: ' + str(w) + ',h: ' + str(h)



    for i in range(h):

        for j in range(w):

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

                ans += 1

                visit(j, i, area)



    print(ans)



if __name__ == "__main__":

    sys.setrecursionlimit(100000)

    while True:

        w , h = 0, 0

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



        if w == 0 and h == 0:

            break



        solve()
def solve_sub(x, y, area):

    stack = []

    stack.append((x, y))



    move = [(x, y) for x in range(-1, 2) for y in range(-1, 2) if not (x == 0 and y == 0)]



    while len(stack) != 0:

        now = stack.pop()

        x = now[0]

        y = now[1]

        area[y][x] = 0



        for dire in move:

            if 0 <= x + dire[0] < w and 0 <= y + dire[1] < h and area[y + dire[1]][x + dire[0]] == 1:

                stack.append((x + dire[0], y + dire[1]))

                area[y + dire[1]][x + dire[0]] = 2





def solve(area):

    ans = 0

    for y in range(h):

        for x in range(w):

            if area[y][x] == 1:

                ans += 1

                solve_sub(x, y, area)



    print(ans)



if __name__ ==  "__main__":

    while True:

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

        if w == 0 and h == 0:

            break



        area = []

        for y in range(h):

            area.append(list(map(int, input().split())))



        solve(area)
import sys


def visit(x, y, area):
    area[y][x] = 0
    move = [(-1, 0), (0, 1), (1, 0), (0, -1), (1, -1), (1, 1), (-1, 1), (-1, -1)]
    # print '(' + str(x) + ',' + str(y) + ')'
    for i in move:
        if (
            0 <= (x + i[0]) < w
            and 0 <= (y + i[1]) < h
            and area[y + i[1]][x + i[0]] == 1
        ):
            visit(x + i[0], y + i[1], area)


def solve():
    area = []
    ans = 0
    for i in range(h):
        area.append(list(map(int, input().split())))
    # print 'w: ' + str(w) + ',h: ' + str(h)
    for i in range(h):
        for j in range(w):
            if area[i][j] == 1:
                ans += 1
                visit(j, i, area)
    print(ans)


if __name__ == "__main__":
    sys.setrecursionlimit(100000)
    while True:
        w, h = 0, 0
        w, h = list(map(int, input().split()))
        if w == 0 and h == 0:
            break
        solve()

def solve_sub(x, y, area):
    stack = []
    stack.append((x, y))
    move = [
        (x, y) for x in range(-1, 2) for y in range(-1, 2) if not (x == 0 and y == 0)
    ]
    while len(stack) != 0:
        now = stack.pop()
        x = now[0]
        y = now[1]
        area[y][x] = 0
        for dire in move:
            if (
                0 <= x + dire[0] < w
                and 0 <= y + dire[1] < h
                and area[y + dire[1]][x + dire[0]] == 1
            ):
                stack.append((x + dire[0], y + dire[1]))
                area[y + dire[1]][x + dire[0]] = 2


def solve(area):
    ans = 0
    for y in range(h):
        for x in range(w):
            if area[y][x] == 1:
                ans += 1
                solve_sub(x, y, area)
    print(ans)


if __name__ == "__main__":
    while True:
        w, h = list(map(int, input().split()))
        if w == 0 and h == 0:
            break
        area = []
        for y in range(h):
            area.append(list(map(int, input().split())))
        solve(area)

-import sys +def solve_sub(x, y, area): +    stack = [] +    stack.append((x, y)) +    move = [ +        (x, y) for x in range(-1, 2) for y in range(-1, 2) if not (x == 0 and y == 0) +    ] +    while len(stack) != 0: +        now = stack.pop() +        x = now[0] +        y = now[1] +        area[y][x] = 0 +        for dire in move: +            if ( +                0 <= x + dire[0] < w +                and 0 <= y + dire[1] < h +                and area[y + dire[1]][x + dire[0]] == 1 +            ): +                stack.append((x + dire[0], y + dire[1])) +                area[y + dire[1]][x + dire[0]] = 2 -def visit(x, y, area): -    area[y][x] = 0 -    move = [(-1, 0), (0, 1), (1, 0), (0, -1), (1, -1), (1, 1), (-1, 1), (-1, -1)] -    # print '(' + str(x) + ',' + str(y) + ')' -    for i in move: -        if ( -            0 <= (x + i[0]) < w -            and 0 <= (y + i[1]) < h -            and area[y + i[1]][x + i[0]] == 1 -        ): -            visit(x + i[0], y + i[1], area) - - -def solve(): -    area = [] +def solve(area): -    for i in range(h): -        area.append(list(map(int, input().split()))) -    # print 'w: ' + str(w) + ',h: ' + str(h) -    for i in range(h): -        for j in range(w): -            if area[i][j] == 1: +    for y in range(h): +        for x in range(w): +            if area[y][x] == 1: -                visit(j, i, area) +                solve_sub(x, y, area) -    sys.setrecursionlimit(100000) -        w, h = 0, 0 -        solve() +        area = [] +        for y in range(h): +            area.append(list(map(int, input().split()))) +        solve(area)