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 dfs():
    while len(stack) > 0:
        height, width = stack.pop()
        for k in range(-1, 2):
            for l in range(-1, 2):
                new_h, new_w = height + k, width + l
                if new_h < 0 or new_w < 0 or new_h >= h or new_w >= w:
                    continue
                #訪れていない場所で，そこが島だったら
                if visited[new_h][new_w] == 0 and c[new_h][new_w] == 1:
                    visited[new_h][new_w]  = 1
                    stack.append([new_h, new_w])

    return 1

#実行部分
while True:
    w, h = map(int, input().split())
    #入力が終わるとき
    if w == 0 and h == 0:
        break
    else:
        c = [list(map(int, input().split())) for _ in range(h)]
        visited = [[0] * w for _ in range(h)]
        ans = 0

        #実行部分
        for i in range(h):
            for j in range(w):
                if visited[i][j] == 0 and c[i][j] == 1:
                    visited[i][j] = 1
                    stack = [[i, j]]
                    ans += dfs()
        print(ans)

Python: import sys
sys.setrecursionlimit(10 ** 9)
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)]
    seen = [[False for _ in range(w)] for _ in range(h)]

    def dfs(x, y, seen):
        if not (0 <= x < h and 0 <= y < w):
            return
        if seen[x][y]:
            return
        if c[x][y] == 0:
            return
        seen[x][y] = True
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                dfs(x + dx, y + dy, seen)

        return seen

    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1 and seen[i][j] == False:
                cnt += 1
                dfs(i, j, seen)

    print(cnt)

Python: import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

class Ans():

    def __init__(self, H, W, mat):
        self.H = H
        self.W = W
        self.mat = mat
        self.visited = [[False] * W for _ in range(H)]

    def dfs(self, h, w):
        if self.mat[h][w] == 0 or self.visited[h][w] == True:
            return
        
        self.visited[h][w] = True
        for dh, dw in directions:
            if not (0 <= h + dh < self.H and 0 <= w + dw < self.W):
                continue
            if self.mat[h+dh][w+dw] == 0 or self.visited[h+dh][w+dw] == True:
                continue
            self.dfs(h+dh, w+dw)
        return

def main():
    while 1:
        W, H = map(int, readline().rstrip().split())
        if W == H == 0:
            break

        mat = [list(map(int, readline().rstrip().split())) for _ in range(H)]
        ans = Ans(H, W, mat)

        res = 0
        for h in range(H):
            for w in range(W):
                if ans.mat[h][w] == 1 and ans.visited[h][w] == False:
                    res += 1
                ans.dfs(h, w)

        print(res)

if __name__ == '__main__':
    main()

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