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(1000000)
moves = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))

def dfs(y, x):
  c[y][x] = 0

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

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

Python: import sys
sys.setrecursionlimit(100000)


def dfs(h, w):
    checked[h][w] = 1
    for hi in range(-1, 2):
        for wj in range(-1, 2):
            if 0 <= h+hi <= H-1 and 0 <= w+wj <= W-1:
                if hw[h+hi][w+wj] == 1 and checked[h+hi][w+wj] == -1:
                    dfs(h+hi, w+wj)


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

    hw = []
    for _ in range(H):
        tmp = list(map(int, input().split()))
        hw.append(tmp)

    checked = [[-1] * W for i in range(H)]
    ans = 0
    for w in range(W):
        for h in range(H):
            if hw[h][w] == 1 and checked[h][w] == -1:
                ans += 1
                dfs(h, w)
    print(ans)

Python: #AOJ1160
def search(w,h,data):
    for i in range(h):
        if 1 in data[i]:
            return (i,data[i].index(1))
    return (-1,-1)

def main(w,h):
    data=[list(map(int,input().split())) for j in range(h)]
    start=search(w,h,data)
    island=0
    while start!=(-1,-1):
        island+=1
        checking=[start]
        data[start[0]][start[1]]=0
        while len(checking)>0:
            now=checking.pop()
            for j in range(now[0]-1,now[0]+2):
                for k in range(now[1]-1,now[1]+2):
                    if (j,k)!=now and j>=0 and k>=0 and j<h and k<w:
                        if data[j][k]==1:
                            data[j][k]=0
                            checking.append((j,k))
        start=search(w,h,data)
    return island          

w,h=map(int,input().split())
while w!=0 and h!=0:
    print(main(w,h))
    w,h=map(int,input().split())

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: # AOJ 1160: How Many Islands?
# Python3 2018.7.15 bal4u

# UNION-FIND library
class UnionSet:
	def __init__(self, nmax):
		self.size = [1]*nmax
		self.id = [i for i in range(nmax+1)]
	def root(self, i):
		while i != self.id[i]:
			self.id[i] = self.id[self.id[i]]
			i = self.id[i]
		return i
	def connected(self, p, q): return self.root(p) == self.root(q)
	def unite(self, p, q):
		i, j = self.root(p), self.root(q)
		if i == j: return
		if self.size[i] < self.size[j]:
			self.id[i] = j
			self.size[j] += self.size[i]
		else:
			self.id[j] = i
			self.size[i] += self.size[j]
# UNION-FIND library

def id(r, c):
	global w
	return r*w+c

mv = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]

while True:
	w, h = map(int, input().split())
	if w == 0: break
	arr = [[] for r in range(h)]
	u = UnionSet(w*h)
	for r in range(h): arr[r] = list(map(int, input().split()))
	for r in range(h):
		for c in range(w):
			if not arr[r][c]: continue
			for i in range(8):
				r2, c2 = r + mv[i][0], c + mv[i][1]
				if r2 < 0 or r2 >= h or c2 < 0 or c2 >= w or not arr[r2][c2]: continue
				u.unite(id(r, c), id(r2, c2))
	ans = 0
	for r in range(h):
		for c in range(w):
			if arr[r][c] and u.root(id(r, c)) == id(r, c): ans += 1
	print(ans)

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)