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
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)


def search(i, j):
    global ans
    if c[i][j] == 1:
        c[i][j] = -1
        search(i, j+1)
        search(i+1, j-1)
        search(i+1, j)
        search(i+1, j+1)
        search(i, j-1)
        search(i-1, j)
        search(i-1, j+1)
        search(i-1, j-1)


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[] for _ in range(h + 2)]
    c[0] = [0] * (w + 2)
    c[-1] = [0] * (w + 2)
    for i in range(h):
        c[i + 1] = [0] + list(map(int, input().split())) + [0]
    ans = 0
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if c[i][j] == 1:
                ans += 1
                search(i, j)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dx,dy in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
        search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(h, w, field, seen, dhw):
    seen[h][w] = True
    for dh, dw in dhw:
        nh = h + dh
        nw = w + dw

        if field[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(nh, nw, field, seen, dhw)


def main():
    res = []
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break

        field = []
        field.append([0] * (w + 2))
        for i in range(h):
            line = list(map(int, input().split()))
            field.append([0] + line + [0])
        field.append([0] * (w + 2))

        seen = [[False] * (w + 2) for x in range(h + 2)]
        dhw = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (-1, 1), (1, -1)]

        count = 0
        for ih in range(1, h+2):
            for iw in range(1, w+2):
                if seen[ih][iw]:
                    continue
                if field[ih][iw] == 1:
                    dfs(ih, iw, field, seen, dhw)
                    count += 1
        res.append(count)

    for i in res:
        print(i)


main()

Python: import sys
sys.setrecursionlimit(10000)
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]


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

    V[y][x] = True
    for i in range(8):
        dfs(y + dy[i], x + dx[i])


while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    C = [list(map(int, input().split())) for _ in range(h)]
    V = [[False]*w for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if C[y][x] == 0:
                continue
            if V[y][x]:
                continue
            dfs(y, x)
            ans += 1
    print(ans)

Python: import sys
sys.setrecursionlimit(10**9)
H = 0
W = 0
field = []
def dfs(x, y, px, py):
    for dx, dy in ((1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)):
        dx += x
        dy += y
        if dx == px and dy == py: continue
        if not (0 <= dx < W and 0 <= dy < H): continue
        if field[dy][dx] == 1:
            field[dy][dx] = 2
            dfs(dx, dy, x, y)

while True:
    W, H = map(int, input().split())
    if H+W == 0: break
    ans = 0
    field = [list(map(int, input().split())) for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if field[h][w] == 1:
                ans += 1
                dfs(w,h,-1,-1)
    print(ans)

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

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

def dfs(C,h,w):
    global seen
    seen[h][w] = True
    for dir in range(8):
        nh = h + dx[dir]
        nw = w + dy[dir]
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if C[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs(C,nh,nw)
    
while True:
    W,H = map(int, input().split())
    if W==0 and H==0:
        break
    C = []
    for i in range(H):
        array = list(map(int, input().split()))
        C.append(array)
    
    count = 0
    seen = [[False]*W for _ in range(H)]
    for dh in range(H):
        for dw in range(W):
            if C[dh][dw] != 0 and seen[dh][dw] == False:
                dfs(C,dh,dw)
                count += 1
    
    print(count)

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

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

    print(count)