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: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour - {0}) > 1:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    for row in c:
        print(row)
    print(islands)
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))

Python: move = [[0, 1], [0, -1], [-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]]
from collections import deque
def main(w, h):
    c = [list(map(int, input().split())) for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x]:
                ans += 1
                q = deque()
                q.append((x, y))
                while q:
                    xx, yy = q.popleft()
                    for mx, my in move:
                        xm = xx + mx
                        ym = yy + my
                        if 0 <= xm < w and 0 <= ym < h:
                            if c[ym][xm]:
                                c[ym][xm] = 0
                                q.append((xm,ym))
    print(ans)


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

Python: import sys

# 再帰の深さの上限変更
sys.setrecursionlimit(10**4)

def DFS(v,visit):
  visit[v] = True
  for u in adj[v]:
    if visit[u] == False:
      DFS(u,visit)

while(1):
  W, H = map(int, input().split())
  if (W,H) == (0,0):# 入力終了
    sys.exit()
  
  C = []
  for _ in range(H):
    tmp = list(map(int, input().split()))
    C.append(tmp)

  # 縦横斜め8方向が島か探索し、隣接リストを作成する
  dx = [1,-1,0,0,1,-1,-1,1]
  dy = [0,0,1,-1,1,1,-1,-1]
  
  # 隣接リスト
  adj = [[] for i in range(H*W)]
  
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1:# 陸である
        # 周りが陸か探索する
        tmp = []
        for k in range(8):
          x,y = j+dx[k], i+dy[k]
          if 0 <= y < H and 0 <= x < W:
            if C[y][x] == 1:
              tmp.append(W*y + x)
        adj[W*i+j] = tmp
  # print(adj)
        
  # 深さ優先探索を行う
  # 訪問チェック
  visit = [False for i in range(H*W)]
  
  ans = 0
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1 and visit[W*i+j] == False:
        DFS(W*i+j,visit)
        ans += 1
  print(ans)

Python: from queue import Queue

def dfs(x,y,c):
    que = Queue()
    que.put((x, y))

    while not que.empty():
        x, y = que.get()
        if c[x][y] == 0:
            continue

        c[x][y] = 0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                sx, sy = x + dx, y + dy
                if sx < 0 or sx >= h or sy < 0 or sy >= w:
                    continue
                que.put((sx, sy))
    return c

while True:
    w,h = map(int,input().split())
    if w == h == 0:
        break
    wh = w * h
    c = [list(map(int,input().split())) for _ in range(h)]
    ans = 0
    for x in range(h):
        for y in range(w):
            if c[x][y] == 0:
                continue
            ans += 1
            c = dfs(x,y,c)
    print(ans)