Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

Python: import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))


def dfs(i, j, c):
#    print('dfs:')
#    pri(c)
    if i<0 or j<0 or i>=len(c) or j>=len(c[0]) or c[i][j]==0:
        return
    c[i][j] = 0

    for di in [-1, 0, 1]:
        for dj in [-1, 0, 1]:
            dfs(i+di, j+dj, c)
    
    return

def solve(w, h, c):
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                cnt += 1
                dfs(i, j, c)
    print(cnt)
    return


while True:
    w, h = map(int, input().split())
    c = [list(map(int, input().split())) for _ in range(h)]
    if w == h == 0:
        break
    solve(w, h, c)

Python: import sys

def dfs(i, j):
    todo.append([i, j])
    while(todo != []):
        v = todo.pop(len(todo)-1)
        seen[v[0]][v[1]] = 1
        for i in range(8):
            if m[v[0]+dx[i]][v[1]+dy[i]] == 1 and seen[v[0]+dx[i]][v[1]+dy[i]] == 0:
                todo.append([v[0]+dx[i], v[1]+dy[i]])

while(1):
    w, h = map(int, input().split())
    if not w+h:
        break
    m = [[0] * (w+2) for i in range(h+2)]
    for j in range(h):
        m[j+1][1:w+1] = [int(i) for i in input().split()]

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

    seen = [[0] * (w+2) for i in range(h+2)]
    todo = []
    ans = 0

    for i in range(h+2):
        for j in range(w+2):
            if m[i][j] == 1 and seen[i][j] == 0:
                ans += 1
                dfs(i, j)

    print(ans)

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

import sys

sys.setswitchinterval(10**9)


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: def dfs(w,h,W,H,ID,islands,islandsID):
    if islands[h][w]==1 and islandsID[h][w]!=0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=islandsID[h][w]
    if islands[h][w]==1 and islandsID[h][w]==0:
        for i in range(-1,2):
            for j in range(-1,2):
                dw=w+i
                dh=h+j
                if dw>=0 and dw<W and dh>=0 and dh<H:
                    if islands[dh][dw]==1:
                        islandsID[dh][dw]=ID
        ID+=1
    return ID

while True:
    islands=[]
    islandsID=[]
    ID=1
    W,H=map(int,input().split())
    if W==0:
        break
    for i in range(H):
        islandsID.append([0 for j in range(W)])
    for _ in range(H):
        islands.append(list(map(int,input().split())))
    for h in range(H):
        for w in range(W):
            ID=dfs(w,h,W,H,ID,islands,islandsID)
    print(ID-1)

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

def search_island(h, w, max_h, max_w):
    for i in range(8):
        next_h = h + [1, 1, 0, -1, -1, -1, 0, 1][i]
        next_w = w + [0, 1, 1, 1, 0, -1, -1, -1][i]
        if not 0 <= next_h < max_h or not 0 <= next_w < max_w:
            continue
        elif step[next_h][next_w] or not field[next_h][next_w]:
            continue
        step[next_h][next_w] = True
        search_island(next_h, next_w, max_h, max_w)

while True:
    max_w, max_h = map(int, input().split())
    if max_w == max_h == 0:
        break
    field = [[int(i) for i in input().split()] for _ in range(max_h)]
    step = [[False for _ in range(max_w)] for _ in range(max_h)]
    island = 0
    for i in range(max_h):
        for j in range(max_w):
            if step[i][j] or not field[i][j]:
                continue
            search_island(i, j, max_h, max_w)
            island += 1
    print(island)

Python: import sys

sys.setrecursionlimit(1000000)


def rec(y, x, f):
    f[y][x] = '0'
    for dy, dx in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0),
            (1, 1)]:
        if 0 <= y+dy < len(f) and 0 <= x+dx < len(f[0]) and f[y+dy][x+dx] == '1':
            rec(y+dy, x+dx, f)


def main():
    while True:
        w, h = [int(i) for i in input().split()]
        if w == 0 and h == 0:
            break
        field = []
        for i in range(h):
            field.append(input().split())
        ans = 0
        for i in range(h):
            for j in range(w):
                if field[i][j] == '1':
                    rec(i, j, field)
                    ans += 1
        print(ans)


if __name__ == '__main__':
    main()

Python: import sys
sys.setrecursionlimit(4100000)


def dfs(cy, cx):
    # 一回走らせたら一つの島が消滅すると考える
    if not (cy < 0 or cy >= H or cx < 0 or cx >= W or gridgraph[cy][cx] == "0"):
        gridgraph[cy][cx] = "0"
        for dy, dx in adjacent:
            ny, nx = cy+dy, cx+dx
            dfs(ny, nx)


adjacent = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    gridgraph = [list(map(str, input().split())) for _ in range(H)]
    ans = 0
    for i in range(H):
        for j in range(W):
            if gridgraph[i][j] == "1":
                dfs(i, j)
                ans += 1
    print(ans)

Python: while 1:
	W, H = map(int, input().split())
	field = [input().split() for i in range(H)]
	if W == 0 and H == 0:
		break
	
	searched = [[0]*W for i in range(H)]
	stack = []
	count = 0
	for y in range(H):
		for x in range(W):
			if searched[y][x] == 0:
				searched[y][x] = 1
				if field[y][x] == '1':
					stack += [[x, y]]
					count += 1
					while (stack):
						sx, sy = stack.pop()
						for m in range(8):
							ny = sy + [0,1,1,1,0,-1,-1,-1][m]
							nx = sx + [1,1,0,-1,-1,-1,0,1][m]
							if (ny >= 0 and ny < H) and (nx >= 0 and nx < W):
								if (searched[ny][nx] == 0) and (field[ny][nx] == '1'):
									searched[ny][nx] = 1
									stack += [[nx, ny]]
	else:
		print(count)