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: 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)

Python: import sys
sys.setrecursionlimit(10**6)

def dfs(pos):
  i = pos[0]
  j = pos[1]
  seen[j][i] = True
  for k in range(-1+i,2+i):
    for l in range(-1+j,2+j):
      if 0 <= k < w and 0 <= l < h:
        if field[l][k] == 1 and (not seen[l][k]):
          dfs([k,l])

while True:
  w, h = map(int,input().split())
  if w == 0 and h == 0:
    break
  field = [list(map(int,input().split())) for _ in range(h)]
  seen = [[False]*w for _ in range(h)]
  count = 0
  for i in range(w):
    for j in range(h):
      if field[j][i] == 1 and (not seen[j][i]):
        dfs([i,j])
        count += 1
  print(count)

Python: import sys
sys.setrecursionlimit(100000)

def dfs(i, j):
    g[i][j] = 0

    for x in range(-1, 2):
        for y in range(-1, 2):
            dx = x + i
            dy = y + j

            if 0<=dx<h and 0<=dy<w and g[dx][dy] == 1:
                dfs(dx, dy)
    return

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

    g = [list(map(int, input().split())) for _ in range(h)]

    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(i, j)
                ans += 1

    print(ans)

Python: m = []

def walk(i,j):
    stack = []
    stack.append((i,j))
    while stack:
        p = stack.pop()
        pi = p[0]
        pj = p[1]
        m[pi][pj] = 0
        for k in range(-1,2):
            for l in range(-1,2):
                a = pi+k
                b = pj+l
                if a < 0 or b < 0 \
                        or a >= len(m) or b >= len(m[a]):
                            pass
                elif m[a][b] == 0:
                    pass
                else:
                    stack.append((a,b))

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: import sys
sys.setrecursionlimit(10 ** 6)
islands = []
visited_list = []
w_list = []
h_list = []
while True:
    w ,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    w_list.append(w)
    h_list.append(h)
    island = [list(input().split()) for _ in range(h)]
    islands.append(island)
    visited_list.append([[False] * w for _ in range(h)])

dx = [0,1,1, 1, 0,-1,-1,-1]
dy = [1,1,0,-1,-1,-1, 0, 1]

def dfs(island_m ,now_row , now_column , idx):
    if now_row < 0 or h_list[idx] <= now_row or now_column < 0 or w_list[idx] <= now_column or island_m[now_row][now_column] == "0":
        return
    if visited_list[idx][now_row][now_column]:
        return
    visited_list[idx][now_row][now_column] = True
    for i in range(8):
        dfs(island_m,now_row + dx[i] ,now_column + dy[i],idx)


for m in range(len(islands)):
    cnt = 0
    for row in range(h_list[m]):
        for column in range(w_list[m]):
            if not visited_list[m][row][column] and islands[m][row][column] == "1":
                dfs(islands[m] , row , column , m)
                cnt += 1
    print(cnt)