import sys

sys.setrecursionlimit(10**6)



def erase(x, y):

    c[y][x] = '0'

    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):

        nx, ny = x + dx, y + dy

        if 0 <= nx <= w -1 and 0 <= ny <= h - 1:

            if c[ny][nx] == '1':

                erase(nx, ny)



while True:

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

    if w == h == 0:

        break

    c = [[i for i in input().split()] for _ in range(h)]

    ans = 0

    for y in range(h):

        for x in range(w):

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

                ans += 1

                erase(x, y)

    print(ans)
import sys

sys.setrecursionlimit(10**5)



def erase(x, y):

    c[y][x] = '0'

    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):

        nx, ny = x + dx, y + dy

        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':

            erase(nx, ny)



while True:

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

    if w == h == 0:

        break

    c = [[i for i in input().split()] for _ in range(h)]

    ans = 0

    for y in range(h):

        for x in range(w):

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

                ans += 1

                erase(x, y)

    print(ans)
import sys

sys.setrecursionlimit(10**6)


def erase(x, y):
    c[y][x] = "0"
    for dx, dy in (
        (1, 0),
        (1, 1),
        (0, 1),
        (-1, 1),
        (-1, 0),
        (-1, -1),
        (0, -1),
        (1, -1),
    ):
        nx, ny = x + dx, y + dy
        if 0 <= nx <= w - 1 and 0 <= ny <= h - 1:
            if c[ny][nx] == "1":
                erase(nx, ny)


while True:
    w, h = list(map(int, input().split()))
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == "1":
                ans += 1
                erase(x, y)
    print(ans)

import sys

sys.setrecursionlimit(10**5)


def erase(x, y):
    c[y][x] = "0"
    for dx, dy in (
        (1, 0),
        (1, 1),
        (0, 1),
        (-1, 1),
        (-1, 0),
        (-1, -1),
        (0, -1),
        (1, -1),
    ):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == "1":
            erase(nx, ny)


while True:
    w, h = list(map(int, input().split()))
    if w == h == 0:
        break
    c = [[i for i in input().split()] for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == "1":
                ans += 1
                erase(x, y)
    print(ans)

-sys.setrecursionlimit(10**6) +sys.setrecursionlimit(10**5) -        if 0 <= nx <= w - 1 and 0 <= ny <= h - 1: -            if c[ny][nx] == "1": -                erase(nx, ny) +        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == "1": +            erase(nx, ny)