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: while True:
    w,h=map(int,input().split())
    if h==0 and w==0:
        break
    c=[list(map(int,input().split())) for _ in [0]*h]
    
    visit=[[c[i][j]==0 for j in range(w)] for i in range(h)]
    d=[(1,0),(-1,0),(0,1),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
    cnt=0    
    for i in range(h):
        for j in range(w):
            if visit[i][j]==True:
                continue
            q=[[i,j]]
            visit[i][j]=True
            while q:
                k,l=q.pop()
                for dx,dy in d:
                    if not ((0<=k+dx<h) and (0<=l+dy<w)):
                        continue
                    if visit[k+dx][l+dy]==True:
                        continue
                    q.append([k+dx,l+dy])
                    visit[k+dx][l+dy]=True
            cnt+=1            
    print(cnt)

Python: m = []

def walk(i,j):
    if i < 0 or j < 0 \
            or i >= len(m) or j >= len(m[i]):
                return
    if m[i][j] == 0:
        return
    m[i][j] = 0
    for k in range(-1,2):
        for l in range(-1,2):
            walk(i+k,j+l)

while True:
    w, h = map(int, input().split())
    if w == 0: break
    for i in range(h):
        m.append(list(map(int, input().split())))
    ans = 0
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] == 1:
                walk(i,j)
                ans += 1
    print(ans)
    m = []

Python: from collections import deque
def dfs(G,v,h,w):
    stack = deque([v])
    connected = set([v])
    while stack:
        u = stack.popleft()
        x,y = u
        dx = [-1,0,1]
        dy = [-1,0,1]
        for i in range(3):
            for j in range(3):
                s = x + dx[i]
                t = y + dy[j]
                if 0 <= s < h and 0 <= t < w:
                    if G[s][t] == 1:
                        if (s,t) not in connected:
                            stack.appendleft((s,t))
                            connected.add((s,t))
    return connected

def solve(C,h,w):
    node = set()
    ans = 0
    for i in range(h):
      for j in range(w):
        if C[i][j] == 1 and (i,j) not in node:
          node = node|dfs(C,(i,j),h,w)
          ans += 1
    print(ans)

def main():
    h = 0
    w = 0
    while h >= 0 and w >= 0:
        w,h = map(int,input().split(" "))
        if h == w == 0:
            return
        C = [list(map(int,input().split(" "))) for _ in range(h)]
        solve(C,h,w)
  

if __name__ == "__main__":
    main()

Python: def solve():
    w, h = [int(x) for x in input().split()]
    if w == h == 0:
        return False
    field = [[int(x) for x in input().split()] for _ in range(h)]

    ans = 0
    for x in range(w):
        for y in range(h):
            if field[y][x] > 0:
                stack = []
                mark(field, w, h, x, y, stack)
                while stack:
                    p, q = stack.pop()
                    mark(field, w, h, p, q, stack)
                    
                ans += 1
    print(ans)
    return True



def mark(field, w, h, x, y, stack):
    if x < 0 or x >= w or y < 0 or y >= h:
        return

    if field[y][x] != 1:
        return

    field[y][x] = -1

    stack.append((x+1, y+1))
    stack.append((x+1, y))
    stack.append((x+1, y-1))
    stack.append((x, y+1))
    stack.append((x, y))
    stack.append((x, y-1))
    stack.append((x-1, y+1))
    stack.append((x-1, y))
    stack.append((x-1, y-1))
    

while solve():
    pass

Python: while 1:
    W, H = list(map(int,input().split()))
    if W == 0:break

    C = [list(map(int, input().split())) for i in range(H) ]

    def work(x,y):
        ax = [-1, 0, 1, -1, 1, -1, 0, 1]
        ay = [-1, -1, -1, 0, 0, 1, 1, 1]

        if C[y][x] != 1:return False
        C[y][x] = -1 #mark

        for _ax in ax:
            for _ay in ay:
                if 0 <= _ax + x < W and 0 <= _ay + y < H:
                    work(_ax + x, _ay + y)
        return True

    cnt = 0
    for x in range(W):
        for y in range(H):
            cnt += work(x,y)
    print(cnt)

Python: import sys
sys.setrecursionlimit(100000)

def dfs(h, w):
    global fields
    if not (0 <= h < len(fields) and 0 <= w < len(fields[0])):
        return
    if fields[h][w] == "0":
        return
    fields[h][w] = "0"
    for dh in [-1, 0, 1]:
        for dw in [-1, 0, 1]:
            if dh == 0 and dw == 0:
                continue
            dfs(h + dh, w + dw)

while True:
    W, H = tuple(map(int, input().split(" ")))
    if (W, H) == (0, 0):
        break
    fields = []
    for _ in range(H):
        fields.append(input().split(" "))

    ans = 0
    for h in range(H):
        for w in range(W):
            if fields[h][w] == "1":
                dfs(h, w)
                ans += 1
    print(ans)

Python: def f():
  f()
f()

Python: from sys import stdin, setrecursionlimit
input = stdin.readline

setrecursionlimit(100000)

move = ((-1,-1),(-1,0),(-1,1),(0,1),(0,-1),(1,-1),(1,0),(1,1))
def DFS(x,y):
    if mapmatrix[j][i] == 1 and not checked[y][x]:
        checked[y][x] = True
        
        for dx, dy in move:
            X = x + dx; Y = y + dy
            if X >= 0 and X < W and Y >= 0 and Y < H:
                if mapmatrix[Y][X] == 1:
                    DFS(X,Y)
    
while True:
    W,H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    mapmatrix = [None]*H
    for i in range(H):
        mapmatrix[i] = list(map(int, input().split()))
    
    checked = [[False]*W for _ in range(H)]
    
    islandcnt = 0
    for j in range(H):
        for i in range(W):
            if mapmatrix[j][i] == 1 and not checked[j][i]:
                DFS(i,j)
                islandcnt += 1
    
    print(islandcnt)