from itertools import product

while True:

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

    if w == 0:

        break

    a = [[0]*(w+2)] + [[0]+list(map(int, input().split()))+[0] for _ in [0]*h] + [[0]*(w+2)]

    visited = [[0]*(w+2) for _ in [0]*(h+2)]

    ans = 0

    for y in range(1, h+1):

        for x in range(1, w+1):

            if visited[y][x] or not a[y][x]:

                continue

            ans += 1

            visited[y][x] = 1

            q = [(x, y)]

            pop, append = q.pop, q.append

            while q:

                _x, _y = pop()

                for dx, dy in product(list(range(-1, 2)), list(range(-1, 2))):

                    nx, ny = _x+dx, _y+dy

                    if not visited[ny][nx] and a[ny][nx]:

                        visited[ny][nx] = 1

                        append((nx, ny))

    print(ans)

def solve():

    from collections import deque

    

    while True:

        w, h = [int(x)+2 for x in input().split()]

        if w == 2:

            break



        result = 0

        a = ['0']*w

        for _ in [0]*(h-2):

            a += ['0'] + input().split() + ['0']

        a += ['0']*w



        for i in range(w*h):

            if a[i] != '1':

                continue

            a[i] = '0'

            result += 1

            dq = deque([i])

            while dq:

                j = dq.popleft()

                for dest in (j-w-1, j-w, j-w+1, j-1, j+1, j+w-1, j+w, j+w+1):

                    if a[dest] == '1':

                        a[dest] = '0'

                        dq.append(dest)

        print(result)





if __name__ == "__main__":

    solve()

from itertools import product

while True:
    w, h = list(map(int, input().split()))
    if w == 0:
        break
    a = (
        [[0] * (w + 2)]
        + [[0] + list(map(int, input().split())) + [0] for _ in [0] * h]
        + [[0] * (w + 2)]
    )
    visited = [[0] * (w + 2) for _ in [0] * (h + 2)]
    ans = 0
    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if visited[y][x] or not a[y][x]:
                continue
            ans += 1
            visited[y][x] = 1
            q = [(x, y)]
            pop, append = q.pop, q.append
            while q:
                _x, _y = pop()
                for dx, dy in product(list(range(-1, 2)), list(range(-1, 2))):
                    nx, ny = _x + dx, _y + dy
                    if not visited[ny][nx] and a[ny][nx]:
                        visited[ny][nx] = 1
                        append((nx, ny))
    print(ans)

def solve():
    from collections import deque

    while True:
        w, h = [int(x) + 2 for x in input().split()]
        if w == 2:
            break
        result = 0
        a = ["0"] * w
        for _ in [0] * (h - 2):
            a += ["0"] + input().split() + ["0"]
        a += ["0"] * w
        for i in range(w * h):
            if a[i] != "1":
                continue
            a[i] = "0"
            result += 1
            dq = deque([i])
            while dq:
                j = dq.popleft()
                for dest in (
                    j - w - 1,
                    j - w,
                    j - w + 1,
                    j - 1,
                    j + 1,
                    j + w - 1,
                    j + w,
                    j + w + 1,
                ):
                    if a[dest] == "1":
                        a[dest] = "0"
                        dq.append(dest)
        print(result)


if __name__ == "__main__":
    solve()

-from itertools import product +def solve(): +    from collections import deque -while True: -    w, h = list(map(int, input().split())) -    if w == 0: -        break -    a = ( -        [[0] * (w + 2)] -        + [[0] + list(map(int, input().split())) + [0] for _ in [0] * h] -        + [[0] * (w + 2)] -    ) -    visited = [[0] * (w + 2) for _ in [0] * (h + 2)] -    ans = 0 -    for y in range(1, h + 1): -        for x in range(1, w + 1): -            if visited[y][x] or not a[y][x]: +    while True: +        w, h = [int(x) + 2 for x in input().split()] +        if w == 2: +            break +        result = 0 +        a = ["0"] * w +        for _ in [0] * (h - 2): +            a += ["0"] + input().split() + ["0"] +        a += ["0"] * w +        for i in range(w * h): +            if a[i] != "1": -            ans += 1 -            visited[y][x] = 1 -            q = [(x, y)] -            pop, append = q.pop, q.append -            while q: -                _x, _y = pop() -                for dx, dy in product(list(range(-1, 2)), list(range(-1, 2))): -                    nx, ny = _x + dx, _y + dy -                    if not visited[ny][nx] and a[ny][nx]: -                        visited[ny][nx] = 1 -                        append((nx, ny)) -    print(ans) +            a[i] = "0" +            result += 1 +            dq = deque([i]) +            while dq: +                j = dq.popleft() +                for dest in ( +                    j - w - 1, +                    j - w, +                    j - w + 1, +                    j - 1, +                    j + 1, +                    j + w - 1, +                    j + w, +                    j + w + 1, +                ): +                    if a[dest] == "1": +                        a[dest] = "0" +                        dq.append(dest) +        print(result) + + +if __name__ == "__main__": +    solve()