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: import queue

def proceess(start_x,start_y):
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < w and ny < h:
                    print(map[nx][ny])
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        print("nx : " + str(nx) + ", ny : " + str(ny))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 1 and h == 1:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    print(map)

    count = 0
    for i in range(w):
        for j in range(h):
            if map[i][j] == 1:
                print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)

Python: for i in range(10**6):
    W,H=map(int,input().split())
    if H==0 and W==0:
        exit()
    L=[]
    l=[0 for j in range(W+2)]
    L.append(l)
    for h in range(H):
        a=list(map(int,input().split()))
        a=[0]+a+[0]
        L.append(a)
    L.append(l)
    
    P=[[-1 for w in range(W+2)]for h in range(H+2)]
    for h in range(1,H+1):
        for w in range(1,W+1):
            P[h][w]=(h-1)*W+(w-1)
    Size=[[0 for w in range(W+2)]for h in range(H+2)]
    #print(P)
    def find(a):
        x=a%W+1
        y=a//W+1
        if P[y][x]==a:
            return a
        else:
            return find(P[y][x])
    def union(b,c):
        B=find(b)
        C=find(c)
        if B!=C:
            P[C//W+1][C%W+1]=P[B//W+1][B%W+1]
    for h in range(1,H+1):
        for w in range(1,W+1):
            if L[h][w]==1:
                if L[h-1][w-1]==1:
                    union(P[h][w],P[h-1][w-1])
                if L[h-1][w]==1:
                    union(P[h][w],P[h-1][w])
                if L[h][w-1]==1:
                    union(P[h][w],P[h][w-1])
                if L[h-1][w+1]==1:
                    union(P[h][w],P[h-1][w+1])
                if L[h+1][w-1]==1:
                    union(P[h][w],P[h+1][w-1])
                
    cnt=0
    for h in range(1,H+1):
        for w in range(1,W+1):
            P[h][w]=find(P[h][w])
            if L[h][w]==1 and P[h][w]==(h-1)*W+(w-1):
                cnt+=1
    #print(L)
    #print(P)
    print(cnt)

Python: #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

dire = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

def solve(W, H, c):
    ret = 0
    checked = [[0]*W for i in range(H)]
    q = deque()
    for i in range(H):
        for j in range(W):
            if (c[i][j]) and (not checked[i][j]):
                q.append((i, j)) #((now),(before))
                while q:
                    v = q.pop()
                    nowy, nowx = v
                    checked[nowy][nowx] = 1
                    for dy, dx in dire:
                        if (nowy+dy<H) and (nowy+dy>=0) and (nowx+dx<W) and (nowx+dx>=0):
                            if (c[nowy+dy][nowx+dx]) and (not checked[nowy+dy][nowx+dx]):
                                q.append((nowy+dy, nowx+dx))
                ret += 1
    return ret


def main():
    ans = []
    while True:
        W, H = LI()
        if (W==0) and (H==0):
            break
        c = LIR(H)
        ans.append(solve(W, H, c))
    for i in ans:
        print(i)


main()

Python: import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


while 1:
	w, h = MAP()
	if w == 0 and h == 0:
		exit()

	c = [LIST() for _ in range(h)]

	cnt = 0
	check = [[0]*w for _ in range(h)]

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

	for y in range(h):
		for x in range(w):
			if check[y][x] == 0 and c[y][x] == 1:
				cnt += 1
				check[y][x] = cnt
				q = deque([(y, x)])
				while q:
					ny, nx = q.popleft()
					for k in range(8):
						ky = ny + dy[k]
						kx = nx + dx[k]
						if 0 <= ky < h and 0 <= kx < w and check[ky][kx] == 0 and c[ky][kx] == 1:
							check[ky][kx] = cnt
							q.append((ky, kx))

	print(cnt)