Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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)

Python: ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

import sys

sys.setrecursionlimit(4000)
class iland:
    def __init__(self,w,h):
        self.w = w
        self.h = h

        self.c = [[0]*(w+2) for i in range(h+2)] 
        c_tmp = [list(int(i) for i in input().split()) for _ in range(h)]
        for i in range(h):
            for j in range(w):
                self.c[i+1][j+1] = c_tmp[i][j]

        self.d = [[0]*w for i in range(h)]

    def search(self,y,x):
        # 海
        if self.c[y+1][x+1] == 0:
            # self.d[y][x] = -1
            return 0
        # 探索済み
        if self.d[y][x] !=0:
            return 0

        # print(y,x)
        self.d[y][x] = 1
        self.search(y-1,x)
        self.search(y-1,x+1)
        self.search(y,x+1)
        self.search(y+1,x+1)
        self.search(y+1,x)
        self.search(y+1,x-1)
        self.search(y,x-1)
        self.search(y-1,x-1)

        return 1

ans = []
while (1):
    w,h = mi()

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

    l = iland(w,h)

    # 0=umi 1=riku
    land_no = 0
  
    for i in range(h):
        for j in range(w):
            # print(l.d)
            # print(l.c)
            if l.search(i,j):
                land_no += 1
           
    # print(land_no)
    ans.append(land_no)

for i in ans:
    print(i)