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: from itertools import product
while True:
    w, h = map(int, input().split())
    if w == 0:
        break
    a = [[0]*(w+2)] + [[0]+list(map(int, input().split()))+[0] for _ in [0]*h] + [[0]*(w+2)]
    visited = [[0]*(w+2) for _ in [0]*(h+2)]
    ans = 0
    for y in range(1, h+1):
        for x in range(1, w+1):
            if visited[y][x] or not a[y][x]:
                continue
            ans += 1
            visited[y][x] = 1
            q = [(x, y)]
            pop, append = q.pop, q.append
            while q:
                _x, _y = pop()
                for dx, dy in product(range(-1, 2), range(-1, 2)):
                    nx, ny = _x+dx, _y+dy
                    if not visited[ny][nx] and a[ny][nx]:
                        visited[ny][nx] = 1
                        append((nx, ny))
    print(ans)

Python: 
# 深さ優先探索
def dfs(sy, sx, visited, C):
    cango = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
    stack = [[sy, sx]]
    if visited[sy][sx] == 1:#きたことあるならスルー
      return 0
    visited[sy][sx] = 1
    if C[sy][sx] == 0:#もし海ならスルー
      return 0
    while stack:
      y, x = stack[-1]
      stack.pop()
      for i, j in cango:
        if (0 <= y + i < H) and (0 <= x + j < W):
          if (visited[y + i][x + j] == 0) and (C[y + i][x + j] == 1):
            visited[y + i][x + j] = 1
            stack.append([y + i, x + j])
    return 1

def dfs2(W, H, C1):
  ans = 0  
  visited = [[0] * W for i in range(H)]
  C = C1
  for i in range(H):
    for j in range(W):
      ans += dfs(i, j, visited, C)
    
  print(ans)

p = 0
while (p == 0):
#from collecitons import deque
  W, H = map(int, input().split())
  if (W, H) == (0, 0):
    p == 1
    quit()
  C1 = [0] * H
  for i in range(H):
    C1[i] = list(map(int, input().split()))
  dfs2(W, H, C1)

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)