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: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
    
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
    
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                count += 1

    print(count)

Python: #!python
# -*- coding: utf-8 -*-

try:
    import sys

    sys.setswitchinterval(10**8)


    next_s = ((-1, 1), (0, 1), (1, 1),
              (-1, 0), (1, 0),
              (-1, -1), (0, -1), (1, -1))


    def DFS(x, y):
        if not (0 <= x < W and 0 <= y < H):
            return
        if not map_[y * W + x]:
            return

        map_[y * W + x] = 0

        for dx, dy in next_s:
            DFS(x + dx, y + dy)


    while True:
        W, H = map(int, input().split())
        if W == 0 and H == 0:
            break

        map_ = []
        for h in range(H):
            map_ += list(map(int, input().split()))
        island_num = 0
        while any(map_):
            next_idx = map_.index(1)
            x = next_idx % W
            y = next_idx // W
            DFS(x, y)
            island_num += 1

        print(island_num)
except Exception as e:
    print(e)

Python: from collections import deque
def dfs(i,j,seen):
    q=deque()
    q.append((i,j,0))
    seen[i][j]=0
    dx=[0,1,0,-1,-1,-1,1,1]
    dy=[1,0,-1,0,1,-1,1,-1]
    while q:
        X,Y,S=q.pop()
        if S==0:
            #下に遷移していくときの処理
            #q.append((V,P,1))
            for k in range(8):
                nx=X+dx[k]
                ny=Y+dy[k]
                if not(0<=nx<h and 0<=ny<w):continue
                if seen[nx][ny]==0:continue
                seen[nx][ny]=0
                q.append((nx,ny,0))
    return 1

def main(w,h):
    grid=[list(map(int,input().split())) for i in range(h)]
    ans=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]:
                ans+=dfs(i,j,grid)
    print(ans)

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

Python: import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

def dfs(x,y):
    c[x][y]='0'
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx=dx+x
            ny=dy+y
            if c[nx][ny]=='1':dfs(nx,ny)

while 1:
    c=[]
    w,h=map(int,input().split())
    if w==h==0:exit()
    w+=2
    c=[list('@'*w)]
    for i in range(h):
        c+=[list('@'+input().replace(' ','').replace('\n','')+'@')]
    c+=[list('@'*w)]
    h+=2
    cnt=0

    for i in range(1,h):
        for j in range(1,w):
            if c[i][j]=='1':
                dfs(i,j)
                cnt+=1
    print(cnt)

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(4):
        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(10**6)


def lake_counting(field):

  lake_count = 0

  def dfs(x,y):
    field[x][y] = 0
    dx = [-1,-1,-1,0,0,1,1,1]
    dy = [-1,0,1,-1,1,1,0,-1]
    

    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < h and 0 <= ny < w and field[nx][ny] == 1):
          dfs(nx,ny)

  for i in range(h):
    for j in range(w):
      if field[i][j] == 1:
        dfs(i,j)
        lake_count += 1

  return lake_count

while True:
  w, h = map(int,input().split())
  if w == h == 0:
    break
  A = []
  for i in range(h):
    c = list(map(int,input().split()))
    A.append(c)
  print(lake_counting(A))