Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

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)

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)