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: w, h = 0, 0

def dfs(x, y, data):
    data[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
            if data[sx][sy] > 0:
                data = dfs(sx, sy, data)
    return data

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

    data = []
    for i in range(h):
        data.append(list(map(int, input().split())))

    ans = 0
    for x in range(h):
        for y in range(w):
            if data[x][y] > 0:
                ans += 1
                data = dfs(x, y, data)
    print (ans)

Python: #!python
# -*- coding: utf-8 -*-

import sys

import numpy as np

sys.setswitchinterval(10**8)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if not map_[y, x]:
        return

    map_[y, x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break

    map_ = np.array([list(map(int, input().split())) for h in range(H)])
    # print(map_)
    island_num = 0
    while np.amax(map_):
        x = np.argmax(map_) % W
        y = np.argmax(map_) // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print('\t', island_num)

Python: import sys

sys.setrecursionlimit(10**7)

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

    # 0=>海, 1=>陸
    island = [list(input().split()) for _ in range(h)]

    island_number = 0

    def dfs(y, x):
        if x < 0 or w <= x or y < 0 or h <= y:
            return

        if island[y][x] == "0":
            return

        island[y][x] = "0"

        dfs(y, x - 1)
        dfs(y, x + 1)
        dfs(y - 1, x)
        dfs(y + 1, x)
        dfs(y + 1, x + 1)
        dfs(y + 1, x - 1)
        dfs(y - 1, x + 1)
        dfs(y - 1, x - 1)



    # iは横軸 jは縦軸
    for i in range(w):
        for j in range(h):

            if island[j][i] == "1":
                dfs(j, i)
                island_number += 1

    print(island_number)

Python: from collections import deque

move = [(0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1), (1, 0), (-1, 0)]

def count(chizu, start):
    stack = deque([start])
    while stack:
        y, x = stack.pop()
        for dxi, dyi in move:
            if not (0<=x+dxi<w and 0<=y+dyi<h) or chizu[y+dyi][x+dxi] == "0":
                continue
            else:
                stack.append((y+dyi, x+dxi))
        chizu[y][x] = "0"


while True:
    w, h = map(int,input().split())
    if w == 0:
        break
    
    island = [0]*h
    ans = 0
    for i in range(h):
        island[i] = list(input().split())
    
    for r in range(h):
        for c in range(w):
            if island[r][c] == "1":
                count(island, (r, c))
                ans += 1
    print(ans)

Python: #!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

D = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
def solve(w,h):
    def bfs(y,x):
        q = deque([(y,x)])
        while q:
            y,x = q.popleft()
            for dx,dy in D:
                ny,nx = y+dy,x+dx
                if 0 <= ny < h and 0 <= nx < w:
                    if c[ny][nx] and not d[ny][nx]:
                        d[ny][nx] = 1
                        q.append((ny,nx))
    c = LIR(h)
    d = [[0]*w for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] and not d[y][x]:
                ans += 1
                d[y][x] = 1
                bfs(y,x)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    while 1:
        w,h = LI()
        if w == h == 0:
            break
        solve(w,h)

Python: import sys
sys.setrecursionlimit(10**9)
def solve(w,h):
    G = [list(map(int,input().split())) for _ in range(h)]
    seen = [[False]*w for _ in range(h)]
    ans = 0
    def dfs(x,y):
        if seen[y][x]:
            return
        seen[y][x] = True
        for dx in range(-1,2):
            for dy in range(-1,2):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < w and 0 <= ny < h:
                    if G[y][x] == 1:
                        dfs(nx,ny)
    for x in range(w):
        for y in range(h):
            if G[y][x] == 1 and seen[y][x] == False:
                dfs(x,y)
                ans += 1
    print(ans)


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