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
sys.setrecursionlimit(10**7)
vec = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)]
def dfs(x,y,visited,field):
        
     
        visited[x][y] = 1
        for nx,ny in vec:
            tx = x + nx
            ty = y+ny
            if tx < 0 or ty < 0 or tx > h-1 or ty > w-1:continue
            if visited[tx][ty]:continue
            if field[tx][ty] == 0:continue
            dfs(tx,ty,visited,field)
            
        return
while True:    
    w,h = map(int,input().split())
    if w == 0 and h == 0:break
    field = [list(map(int,input().split()))for i in range(h)]
    visited = [[0]*w for i in range(h)]
    ans = 0
    
    for i in range(h):
        for j in range(w):
            if field[i][j] == 1 and visited[i][j] == 0:
                dfs(i,j,visited,field)
                ans +=1
    
    print(ans)

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



def tansaku(i,j):
	for di in range(-1,2):
		for dj in range(-1,2):
			if 0<=i+di<h and 0<=j+dj<w:
					if visited[i+di][j+dj]:
						continue
					else:
						visited[i][j]=1
						if c[i][j]==0:continue
						tansaku(i+di,j+dj)
			else:continue
			
			
	return

			
def dfs(i,j):
	global count
	if visited[i][j]:return
	visited[i][j]=1
	# print(visited)
	if c[i][j]==0:return
	count+=1
	tansaku(i,j)

while True:
	# print('スタート')
	w,h =list(map(int,input().split()))
	if w==0 and h==0:break
	c=[list(map(int,input().split())) for i in range(h)]
	visited=[[0]*w for i in range(h)]
	count=0
	for s in range(w):
		for t in range(h):
			dfs(t,s)

	print(count)

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(2):
        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(100000)


def dfs(x,graph,seen,nxt):
    seen[x]=1
    if x in nxt:
        nxt.remove(x)
    nxt+=[y for y in graph[x] if not seen[y] and y not in nxt]
    while nxt :
        dfs(nxt.pop(-1),graph,seen,nxt)


def island_count(w,h):
    lst=[list(map(int,input().split())) for _ in range(h)]
    graph={}
    for i in range(h):
        for j in range(w):
            if not lst[i][j] : continue
            graph[(i,j)]=[]
            for k in range(-1,2):
                for l in range(-1,2):
                    if k==0 and l==0 : continue
                    if i+k<0 or j+l<0 : continue
                    try:
                        if lst[i+k][j+l]:
                            graph[(i,j)].append((i+k,j+l))
                    except IndexError:
                        continue
    #print(graph)
    seen={x:0 for x in graph}
    sm=0
    nxt=[]
    for x in seen:
        if seen[x] : continue
        if not seen[x]:
            dfs(x,graph,seen,nxt)
            sm+=1

    print(sm)






while 1 :
    w,h=map(int,input().split())
    if (w,h)==(0,0) : break
    island_count(w,h)

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

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

    while True:
        w,h = LI()
        if w == 0:
            break
        a = [LI() for _ in range(h)]
        f = collections.defaultdict(bool)
        r = 0
        for i in range(h):
            for j in range(w):
                if f[(i,j)] or a[i][j] == 0:
                    continue
                r += 1
                q = [(i,j)]
                f[(i,j)] = True
                qi = 0
                while len(q) > qi:
                    ci,cj = q[qi]
                    qi += 1
                    for di,dj in dd:
                        ni = ci + di
                        nj = cj + dj
                        if 0 <= ni < h and 0 <= nj < w and a[ni][nj] == 1 and not f[(ni,nj)]:
                            f[(ni,nj)] = True
                            q.append((ni,nj))
        rr.append(r)

    return '\n'.join(map(str, rr))


print(main())

Python: import sys
sys.setrecursionlimit(50*50)

seen = []
field = []
dx =[0, 1, 1, 1, 0, -1, -1, -1]
dy =[1, 1, 0, -1, -1, -1, 0, 1]
global W, H

def dfs_grid(w, h):
    seen[h][w]=True
    #print(h, w)

    for i in range(8):
        nw = w + dx[i]
        nh = h + dy[i]

        if nw < 0 or nw >= W or nh < 0 or nh >= H:
            continue
        if field[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs_grid(nw, nh)
while True:
    W, H = map(int, input().split())
    cnt =0

    if W ==0 and H ==0:
        break

    for i in range(H):
        field.append(list(map(int,(input().split()))))
        seen.append([False]* W)

    #print(field)
    #print(seen)

    for i in range(H):
        for j in range(W):
            if field[i][j]==0:
                continue
            if not(seen[i][j]):
                dfs_grid(j, i)
                cnt +=1

    #print(seen)
    field.clear()
    seen.clear()
    print(cnt)