Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

Python: #!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

while 1:
    w,h = LI()
    if w == 0 and h == 0:
        break
    c = [LI() for _ in range(h)]
    check = [[True]*w for _ in range(h)]

    dx,dy = [1,1,0,-1,-1,-1,0,1],[0,1,1,1,0,-1,-1,-1]
    ans = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] and check[i][j]:
                que = deque()
                que.append((i,j))
                check[i][j] = False
                ans += 1
                while que:
                    y,x = que.popleft()
                    for k in range(8):
                        ny,nx = y+dy[k],x+dx[k]
                        if 0 <= ny < h and 0 <= nx < w:
                            if check[ny][nx]:
                                if c[ny][nx]:
                                    que.append((ny,nx))
                                    check[ny][nx] = False
    print(ans)

Python: def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x) 
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

Python: dy = (-1,-1,0,1,1,1,0,-1)
dx = (0,1,1,1,0,-1,-1,-1)

def main():
    while True:
        w,h = map(int,input().split())
        if h == 0:
            return
        grid = [list(map(int,input().split())) for _ in range(h)]
        visited = [[0] * w for _ in range(h)]
        ans = 0
        for i in range(h):
            for j in range(w):
                if grid[i][j] == 1 and visited[i][j] == 0:
                    ans += 1
                    stack = [(i,j)]
                    visited[i][j] = 1
                    while stack:
                        p,q = stack.pop()
                        for k in range(8):
                            y = p + dy[k]
                            x = q + dx[k]
                            if y < 0 or y >= h:
                                continue
                            if x < 0 or x >= w:
                                continue
                            if grid[y][x] == 1 and visited[y][x] == 0:
                                stack.append((y,x))
                                visited[y][x] = 1
        print(ans)
if __name__ == '__main__':
    main()

Python: def main():
    W, H = (int(i) for i in input().split())
    while H != 0 or W != 0:
        c = [[int(i) for i in input().split()] for j in range(H)]
        seen = [[False for i in range(W)] for j in range(H)]
        def dfs(G,h,w,seen,H,W):
            todo = []
            hws = ((1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1))
            seen[h][w] = True
            todo.append((h,w))
            # todoが非空の間繰り返し
            while todo:
                h, w = todo[-1]
                for dh,dw in hws:
                    next_h = h + dh
                    next_w = w + dw
                    if not ((0 <= next_h < H) and (0 <= next_w < W)): continue
                    if G[next_h][next_w] == 0: continue
                    if seen[next_h][next_w]: continue
                    seen[next_h][next_w] = True
                    todo.append((next_h,next_w))
                    break
                else:
                    todo.pop()
        ans = 0
        for i in range(H):
            for j in range(W):
                if c[i][j] == 0 or seen[i][j]: continue
                dfs(c,i,j,seen,H,W)
                ans += 1
        print(ans)
        W, H = (int(i) for i in input().split())

if __name__ == '__main__':
    main()

Python: anslist = []
def sarch(i, j, visited):

    if 0 < i:
        if visited[i - 1][j] == 0:
            visited[i - 1][j] = 1
            sarch(i - 1, j, visited)
        if 0 < j:
            if visited[i - 1][j - 1] == 0:
                visited[i - 1][j - 1] = 1
                sarch(i - 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i - 1][j + 1] == 0:
                visited[i - 1][j + 1] = 1
                sarch(i - 1, j + 1, visited)

    if i < len(visited) - 1:
        if visited[i + 1][j] == 0:
            visited[i + 1][j] = 1
            sarch(i + 1, j, visited)
        if 0 < j:
            if visited[i + 1][j - 1] == 0:
                visited[i + 1][j - 1] = 1
                sarch(i + 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i + 1][j + 1] == 0:
                visited[i + 1][j + 1] = 1
                sarch(i + 1, j + 1, visited)

    if 0 < j:
        if visited[i][j - 1] == 0:
            visited[i][j - 1] = 1
            sarch(i, j - 1, visited)
    if j < len(visited[0]) - 1:
        if visited[i][j + 1] == 0:
            visited[i][j + 1] = 1
            sarch(i, j + 1, visited)
    return visited


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    grid = [list(map(int, input().split())) for j in range(h)]
    visited = [[0 for i in range(w)] for j in range(h)]
    starti = 0
    startj = 0
    for i in range(h):
        for j in range(w):
            if grid[i][j] == 0:
                visited[i][j] = -1
            else:
                visited[i][j] = 0

    i = 0
    j = 0
    cnt = 0
    ans = 0
    #print(visited)
    while True:
        if i >= h:
            i = 0
            j = 0
        while True:
            cnt += 1
            if j >= w:
                j = 0
                break
            #print(visited[i][j], 100100)
            if visited[i][j] == 0:
                ans += 1
                visited[i][j] = 1
                visited = sarch(i, j, visited)
                cnt = 0
            j += 1
        i += 1
        #print(cnt, visited)
        if cnt > w * h:
            #print(10)
            break
    anslist.append(ans)

for i in anslist:
    print(i)