Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: import sys
from itertools import product as pro
sys.setrecursionlimit(10**7)

while True:
    w,h=map(int,input().split())
    if w==0 and h==0:
        sys.exit()
    
    G=[]
    for i in range(h):
        G.append(list(map(int,input().split())))

    #隣接する島
    def sur(X):
        x=X[0]
        y=X[1]
        axis_x=[x+1,x,x-1]
        axis_y=[y+1,y,y-1]
        preE=list(pro(axis_x,axis_y))
        preE.remove((x,y))
        E=[]
        for e in preE:
            if 0<=e[0]<=h-1 and 0<=e[1]<=w-1:
                if G[e[0]][e[1]]==1:
                    E.append(e)
        return E

    check=[[False]*w for i in range(h)]

    #checkの陸埋めをしても良い
    def dfs(v):
        check[v[0]][v[1]]=True
        for next in sur(v):
            if check[next[0]][next[1]]:
                continue
            dfs(next)

    ans=0
    for i in range(h):
        for j in range(w):
            if not check[i][j] and G[i][j]==1:
                ans+=1
                dfs((i,j))
    
    print(ans)

Python: import sys
sys.setrecursionlimit(10**6)

w=0;h=0;
def dfs(x,y,Map):
    #ending condition
    if not (0 <= x and x < h and 0 <= y and y < w):
        return
    if Map[x][y] == 0:
        return
    
    #operation
    Map[x][y] = 0

    #moving deep
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            dfs(x+i,y+j,Map)

def start_cnt():
    global w,h
    w,h = map(int,input().split())
    if w == 0 and h == 0:exit();
    cnt = 0
    Map = []
    for i in range(h):
            Map.append(list(map(int,input().split())))
    for i in range(h):
        for j in range(w):
            if Map[i][j] == 1:
                dfs(i,j,Map)
                cnt += 1
    print(cnt)
while True:
    start_cnt()

Python: import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
def dfs(a,v):
    global st,per
    if v in st: st.remove(v)
    else: return
    for tom in per:
        if a[v[0]+tom[0]][v[1]+tom[1]]==1: dfs(a,(v[0]+tom[0],v[1]+tom[1]))
while True:
    c,r=map(int,input().split())
    if c==0 and r==0 : exit(0)
    a=[[0]*(c+2)]
    for j in range(r):
        a.append([0]+[int(i) for i in input().split()]+[0])
    a.append([0]*(c+2))
    st=set()
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j]==1:
                st.add((i,j))
    per=[[1,1],[1,0],[0,1],[-1,1],[1,-1],[-1,-1],[-1,0],[0,-1]]
    ans=0
    while st:
        ans+=1
        for i in st:
            dfs(a,i)
            break
    print(ans)

Python: import sys
sys.setrecursionlimit(10**7)

def dfs(row, column):
    Map[row][column] = num
    for i in range(-1, 2):
        for j in range(-1, 2):
            if Map[row + i][column + j] == 1:
                dfs(row + i, column + j)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    
    Map = [[0] * (w+2)]
    Map += [[0] + list(map(int, input().split())) + [0] for _ in range(h)]
    Map += [[0] * (w+2)]
    
    num = 1
    
    for i in range(h+2):
        for j in range(w+2):
            if Map[i][j] == 1:
                num += 1
                dfs(i,j)
    print(num-1)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(len(dxdy)):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        # if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
        #     if Map[nextPos[0]][nextPos[1]] == 1:
        #         dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

Python: import sys
sys.setrecursionlimit(10 ** 7)

direction = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1))


def dfs(y, x):
    board[y][x] = 0

    for d in direction:
        nh = y + d[1]
        nw = x + d[0]

        if nh < 0 or nh >= h or nw < 0 or nw >= w:
            continue
        if board[nh][nw] == 0:
            continue
        dfs(nh, nw)

while True:
    w, h = map(int, input().split())
    if h == 0 and w == 0:
        break
    board = [list(map(int, input().split())) for i in range(h)]

    cnt = 0
    for y in range(h):
        for x in range(w):
            if board[y][x] == 0:
                continue
            dfs(y, x)
            cnt += 1
    print(cnt)

Python: def dfs(c, x, y, w, h):
    c[y][x] = -1

    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == 0 and j == 0:
                continue
            x_ = x + i
            y_ = y + j
            if 0 <= x_ < w and 0 <= y_ < h and c[y_][x_] == 1:
                dfs(c, x_, y_, w, h)

def solve(w, h):
    c = []
    ans = 0

    for _ in range(h):
        c.append(list(map(int, input().split())))

    for y in range(h):
        for x in range(w):
            if c[y][x] == 1:
                ans += 1
                dfs(c, x, y, w, h)

    print(ans)

if __name__ == "__main__":
    import sys
    sys.setrecursionlimit(10000)
    while True:
        w, h = map(int, input().split())
        if w * h == 0:
            break
        solve(w, h)

Python: 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 = 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)