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: import sys
sys.setrecursionlimit(200000)
readline = sys.stdin.buffer.readline


def dfs(C, h, w):
    # 一度訪ねた陸地は'0'(海)に置き換える
    C[h][w] = '0'
    for i in range(-1, 2):
        for j in range(-1, 2):
            if 0 <= h+i < len(C) and 0 <= w+j < len(C[0]) and C[h+i][w+j] == '1':
                dfs(C, h+i, w+j)


W, H = 1, 1
while W != 0 or H != 0:
    W, H = map(int, input().split())
    C = []
    for h in range(H):
        c = list(input().split())
        #c = readline().split()
        C.append(c)

    count = 0
    for h in range(H):
        for w in range(W):
            if C[h][w] == '1':
                dfs(C, h, w)
                count += 1
    if W != 0 or H != 0:
        print(count)

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

# 移動ベクトル
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]

def dfs(h, w):
    seen[h][w] = True
    
    # 次の探索位置を取得
    for dir in range(8):
        nh, nw = h + dx[dir], w + dy[dir]
        if nh < 0 or nh >= H or nw < 0 or nw >= W: continue
        if field[nh][nw] == 0: continue
        if seen[nh][nw] == True: continue
        dfs(nh, nw)
        
while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    field = [[int(i) for i in input().split()] for j in range(H)]

    seen = [[False for i in range(W)] for j in range(H)]

    count = 0
    for h in range(H):
        for w in range(W):
            if seen[h][w] == False and field[h][w] == 1:
                dfs(h, w)
                count += 1

    print(count)

Python: import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7) 

count_list = []

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

    grid = [0] * (w + 2)
    for _ in range(h):
        grid += [0]
        grid += list(map(int, input().split()))
        grid += [0]
    grid += [0] * (w + 2)

    move = [-1, +1, -(w + 2) - 1, -(w + 2), -(w + 2) + 1, w + 2 - 1, w + 2, w + 2 + 1]
    visit = set()
    count = 0

    def search(x):
        if x not in visit:
            visit.add(x)
            for dx in move:
                y = x + dx
                if grid[y] == 0:
                    continue
                else:
                    search(y)

    for i in range((w+2) * (h+2)):
        if grid[i] == 0 or i in visit:
            continue
        count += 1
        search(i)
    count_list.append(count)

for i in range(len(count_list)):
    print(count_list[i])

Python: def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    main()

Python: 
dirs = [(-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,h) == (0,0):
		break
	tile = [input().split() for i in range(h)]
	count = 0
	for i in range(h):
		for j in range(w):
			if tile[i][j] == '1':
				count += 1
				q = [(i,j)]
				while q:
					(x,y) = q.pop()
					for d in dirs:
						xx = x+d[0]
						yy = y+d[1]
						if (0 <= xx and xx < h) and (0 <= yy and yy < w):
							if tile[xx][yy] == '1':
								tile[xx][yy] = '0'
								q.append((xx,yy))
	print(count)

Python: import sys
sys.setrecursionlimit(2500)
w,h = map(int,input().split())
while not(w==h==0):
    m = [[0]*(w+2)]
    m += [[0]+list(map(int,input().split()))+[0] for i in range(h)]
    m += [[0]*(w+2)]
    g = [[] for i in range(w*h)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if m[i][j]==1:
                for k in [-1,0,1]:
                    for l in [-1,0,1]:
                        if m[i+k][j+l]==1:
                            g[(i-1)*w+j-1].append((i+k-1)*w+j+l-1)
    reach = [False]*(w*h)
    def dfs(v):
        reach[v] = True
        for i in g[v]:
            if not reach[i]:
                dfs(i)
    ans = 0   
    for i in range(h*w):
        if g[i] and not reach[i]:
            ans += 1
            dfs(i)

    print(ans)

    w,h = map(int,input().split())

Python: from collections import deque
import sys
sys.setrecursionlimit(10**6)
def dfs(x,y):
    c[x][y] = 0
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx = x + dx
            ny = y + dy

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