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: ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

import sys

sys.setrecursionlimit(4000)
class iland:
    def __init__(self,w,h):
        self.w = w
        self.h = h

        self.c = [[0]*(w+2) for i in range(h+2)] 
        c_tmp = [list(int(i) for i in input().split()) for _ in range(h)]
        for i in range(h):
            for j in range(w):
                self.c[i+1][j+1] = c_tmp[i][j]

        self.d = [[0]*w for i in range(h)]

    def search(self,y,x):
        # 海
        if self.c[y+1][x+1] == 0:
            # self.d[y][x] = -1
            return 0
        # 探索済み
        if self.d[y][x] !=0:
            return 0

        # print(y,x)
        self.d[y][x] = 1
        self.search(y-1,x)
        self.search(y-1,x+1)
        self.search(y,x+1)
        self.search(y+1,x+1)
        self.search(y+1,x)
        self.search(y+1,x-1)
        self.search(y,x-1)
        self.search(y-1,x-1)

        return 1

ans = []
while (1):
    w,h = mi()

    if(w == 0 and h==0):
        break

    l = iland(w,h)

    # 0=umi 1=riku
    land_no = 0
  
    for i in range(h):
        for j in range(w):
            # print(l.d)
            # print(l.c)
            if l.search(i,j):
                land_no += 1
           
    # print(land_no)
    ans.append(land_no)

for i in ans:
    print(i)

Python: import sys
sys.setrecursionlimit(10000)

def get_two_int():
    two_int = input().split()
    for i in range(2):
        two_int[i] = int(two_int[i])
    return two_int

def get_data_list():
    data_list = input().split()
    for i, v in enumerate(data_list):
        data_list[i] = int(v)
    # print("data_list", data_list) # debug
    return data_list


def make_map(width, height):
    map_list = [[0] * (width + 2) for x in range(height + 2)] # まわりを0で囲む
    # print("map_list", map_list) # debug pprint?
    for h in range(1, height + 1):
        line_list = get_data_list()
        for w in range(1, width + 1):
            map_list[h][w] = line_list[w-1] # 汚い
    #print("map_list", map_list) # debug
    return map_list

def count_island(width, height, map_list):
    count = 0
    checked_set = set()
    for h in range(1, height + 1):
        for w in range(1, width + 1):
            if map_list[h][w] == 1 and ((h, w) not in checked_set):
                map_list, checked_set = check_around(h, w, map_list, checked_set)
                count += 1
    # print("checked_set", checked_set) # debug
    return count

def check_around(h, w, map_list, checked_set):
    # print((h, w)) #debug
    checked_set.add((h, w))
    # print("map_list", map_list) # debug
    for i in range(h - 1, h + 2): # まわりを全てチェック、大枠のまわりを0で余分に囲っているので無問題
        for j in range(w - 1, w + 2):
            if map_list[i][j] == 1 and (i, j) not in checked_set:
                    map_list, checked_set = check_around(i, j, map_list, checked_set)
    return map_list, checked_set


if __name__ == "__main__":
    while True:
        width, height = get_two_int()
        if width == 0:
            break
        # print("width", width) # debug
        # print("height", height) # debug
        map_list = make_map(width, height)
        # print("map_list", map_list) # debug 
        count = count_island(width, height, map_list)
        print(count)

Python: import sys

sys.setrecursionlimit(20000)

def fill(m, h, w, x, y):
    m[y][x] = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            nx = x + i
            ny = y + j
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue
            if m[ny][nx] == 0:
                continue
            fill(m, h, w, nx, ny)

def solve(h, w):
    m = []
    for j in range(h):
        line = list(map(int, input().rstrip().split(' ')))
        m.append(line)
    

    count = 0
    for y in range(h):
        for x in range(w):
            if m[y][x] != 0:
                count += 1
                fill(m, h, w, x, y)

    print(count)

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

    solve(h, w)

Python: if __name__ == '__main__':
    while 1:
        w,h = list(map(int,input().strip().split()))
        if w == h ==0:break
        imap = [list(map(int,input().strip().split())) for _ in range(h)]
        visited = []
        count = 0

        for j in range(h):
            for i in range(w):
                stack =[]
                if not (j,i) in visited and imap[j][i] == 1:
                    count +=1
                    stack.append((j,i))
                    visited.append(((j,i)))
                while len(stack) !=0:
                    y,x = stack.pop()
                    for dx in [-1,0,1]:
                        for dy in [-1,0,1]:
                            if  0<= x + dx < w and 0<= y + dy <h and not (y+dy,x+dx)in visited:
                                if imap[y + dy][x + dx] == 1:
                                    stack.append((y+dy,x+dx))
                                    visited.append(((y+dy,x+dx)))
        print(count)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    if 0<=x<w and 0<=y<h and a[y][x]=='1':
        a[y][x]='0'
        for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:f(x+dx,y+dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[list(input().split()) for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)