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

def ironuri(x, y, t, img,h,w):
    if img[y][x]!=1:
        return 0

    img[y][x]=t
    
    if y<h-1:
        ironuri(x,y+1,t,img,h,w)
    if y>0:
        ironuri(x,y-1,t,img,h,w)
    if x<w-1:
        ironuri(x+1,y,t,img,h,w)
    if x>0:
        ironuri(x-1,y,t,img,h,w)

    if x>0 and y>0:
        ironuri(x-1,y-1,t,img,h,w)
    if x>0 and y<h-1:
        ironuri(x-1,y+1,t,img,h,w)
    if x<w-1 and y>0:
        ironuri(x+1,y-1,t,img,h,w)
    if x<w-1 and y<h-1:
        ironuri(x+1,y+1,t,img,h,w)

    
    return 1
    
while True:
    cheese=[]
    il=2
    w,h=map(int, input().split())
    if w==0 and h==0:
        break
    
    for i in range(h):
        ci=list(map(int, input().split()))
        cheese.append(ci)
    
    for i in range(h):
        for j in range(w):
            if(ironuri(j,i,il,cheese,h,w)):
                il+=1

    print(il-2)

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

import sys

sys.setrecursionlimit(10**6)


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 map_[y][x] == 0:
        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_ = [list(map(int, input().split())) for h in range(H)]
    island_num = 0
    for x in range(W):
        for y in range(H):
            if map_[y][x] == 1:
                DFS(x, y)
                island_num += 1
    print(island_num)

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


def dfs(y,x,h,w):
    graph[y][x] = 0
    for ny,nx in (y+1,x),(y-1,x),(y,x+1),(y,x-1),(y+1,x+1),(y-1,x-1),(y-1,x+1),(y+1,x-1):
        if not (0<=ny<h and 0<=nx<w) or graph[ny][nx] ==0:
            continue
        else:
            dfs(ny,nx,h,w)
    return 

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

Python: # import numpy
import sys

sys.setrecursionlimit(10**6)

digtmp = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]

def dfs(i, j):
    global dfsflag
    if Mapdata[i][j] == 1 and Mapcheck[i][j] == 0:
        Mapcheck[i][j] = 1
        dfsflag = 1
        for tmp in digtmp:
            digh = i + tmp[0]
            digw = j + tmp[1]
            if 0 <= digh < h and 0 <= digw < w:
                dfs(digh, digw)
    else:
        Mapcheck[i][j] = 1

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    Mapdata = []
    for _ in range(h):
        tmp = list(map(int, input().split()))
        Mapdata.append(tmp)
    Mapcheck = [[0] * w for _ in range(h)]
    count = 0
    for i in range(h):
        for j in range(w):
            dfsflag = 0
            dfs(i, j)
            if dfsflag == 1:
                count += 1
    print(count)

Python: from itertools import product
import sys

sys.setrecursionlimit(1000000)

def s(m, x, y):
    m[y][x] = 0

    for dx, dy in product(range(-1, 2), range(-1, 2)):
        if m[y + dy][x + dx]:
            s(m, x + dx, y + dy)

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

    if w == h == 0:
        break

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

    for x, y in product(range(w + 2), range(h + 2)):
        if m[y][x]:
            c += 1
            s(m, x, y)
            
    print(c)

Python: import sys
sys.setrecursionlimit(10 ** 5)
 
def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
            erase(nx, ny)
 
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [input().split() for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

a = [(x, y) for x in range(-1, 2) for y in range(-1, 2) if not (x == 0 and y == 0)]
def dfs(g, i, j):
    g[i][j] = 0
    for dx, dy in a:
        nx = i + dx
        ny = j + dy
        if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == 1:
            dfs(g, nx, ny)

while True:
    w, h = na()
    if w == 0 and h == 0:
        break
    g = [na() for _ in range(h)]
    #seen = [[False] * w for _ in range(h)]
    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(g, i, j)
                ans += 1
    print(ans)