Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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


def DFS(x,y):
    color[x][y]="gray"
    for i,j in [[-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,-1],[1,0],[1,1]]:
        X,Y=x+i,y+j
        if 0<=X<h and 0<=Y<w:
            if C[X][Y]==1 and color[X][Y]=="white":
                DFS(x+i,y+j)
    color[x][y]="black"


while 1:
    w,h=map(int,input().split())
    if w==0 and h==0:break
    C=[list(map(int,input().split())) for _ in range(h)]
    color=[["white" for _ in range(w)] for _ in range(h)]
    cnt=0
    for i in range(h):
        for j in range(w):
            if color[i][j]=="white" and C[i][j]==1:
                DFS(i,j)
                cnt +=1
    print(cnt)

Python: import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

def land_check(y,x,W):
    tansaku = deque()
    tansaku.append([y,x])
    che_land = 0
    while tansaku:
        l_y,l_x = tansaku.pop()
        if l_x in visited_land[l_y]:
            continue
        for i in range(l_x-1,l_x+2):
                for j in range(l_y-1,l_y+2):
                    if 0 <= i <= w-1 and 0 <= j <= h-1  and W[j][i] == 1:
                        tansaku.append([j,i])
        visited_land[l_y].add(l_x)
        che_land += 1
    return che_land


def answer(h,w):

    res = 0
    ans = 0
    for i in range(h):
        for j in range(w):
            if tizu[i][j] == 0 or j in visited_land[i]:
                continue
            res += land_check(i,j,tizu)
            ans += 1
            if res == land_count:
                break
        else:
            continue
        break
    return ans

if __name__ == '__main__':
    kotae = []
    while True:
        w,h = nm()
        if [h,w] == [0,0]:
            break
        visited_land = {i:set() for i in range(h)}
        tizu = []
        land_count = 0
        for i in range(h):
            temp = nl()
            land_count += temp.count(1)
            tizu.append(temp)
        kotae.append(answer(h,w))
    [print(i) for i in kotae]

Python: def solve(H, W, C):
	cnt = 0
	d = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]

	while True:
		start = [-1,-1]
		for i in range(H):
			for j in range(W):
				if C[i][j] == 1:
					start = [i,j]
					break

		if start == [-1,-1]:
			return cnt
		else:
			cnt += 1
			q = [start]


		while q:
			cx,cy = q.pop()
			C[cx][cy] = 0
			for dx,dy in d:
				nx,ny = cx+dx, cy+dy
				if 0 <= nx < H and 0 <= ny < W:
					if C[nx][ny] == 1:
						q.append([nx,ny])


ans = []
while True:
	W,H = map(int, input().split())
	if H == 0 and W == 0:
		break
	C = [list(map(int, input().split())) for _ in range(H)]
	#print(H,W,C)
	ans.append(solve(H,W,C))




for a in ans:
	print(a)

Python: def dfs(field, i, j, w, h):
    field[i][j] = "0"
    
    OFFSETS = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
    for di, dj in OFFSETS:
        if i+di < 0 or i+di >= h:
            continue
        if j+dj < 0 or j+dj >= w:
            continue
        if field[i+di][j+dj] == "1":
            dfs(field, i+di, j+dj, w, h)


def main():
    while True:
        w, h = [int(s) for s in input().strip().split()]

        if w == 0 and h == 0:
            break

        num_island = 0
    
        field = []
        for _ in range(h):
            field.append(input().strip().split())
       
        #print(field) 
        for i in range(h):
            for j in range(w):
                if field[i][j] == "1":
                    num_island += 1
                    dfs(field, i, j, w, h)
        
        print(num_island)


if __name__ == "__main__":
    main()

Python: from collections import deque
while True:
    W,H = map(int,input().split())
    if W == 0: break
    src = [list(map(int,input().split())) for i in range(H)]
    visited = [[0 for x in range(W)] for y in range(H)]
    dxy = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
    ans = 0
    for y in range(H):
        for x in range(W):
            if visited[y][x] or not(src[y][x]): continue
            ans += 1
            visited[y][x] = 1
            q = deque([(x,y)])
            while q:
                x0,y0 = q.popleft()
                for dx,dy in dxy:
                    nx,ny = x0+dx, y0+dy
                    if not(0 <= nx < W) or not(0 <= ny < H): continue
                    if visited[ny][nx] or not(src[ny][nx]): continue
                    visited[ny][nx] = 1
                    q.append((nx,ny))
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    global w,h,c
    if x<0 or x>=h or y<0 or y>=w or c[x][y]==0:
        return
    c[x][y]=0
    for a in [-1,0,1]:
        for b in [-1,0,1]:
            if a==0 and b==0:
                continue
            f(x+a,y+b)
while True:
    w,h = map(int,input().split())
    if w+h==0:
        break
    c = [list(map(int,input().split())) for _ in range(h)]
    ans = 0
    for x in range(h):
        for y in range(w):
            if c[x][y]==1:
                ans+=1
                f(x,y)
    print(ans)