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: #!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

while 1:
    w,h = LI()
    if w == 0 and h == 0:
        break
    c = [LI() for _ in range(h)]
    check = [[True]*w for _ in range(h)]

    dx,dy = [1,1,0,-1,-1,-1,0,1],[0,1,1,1,0,-1,-1,-1]
    ans = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] and check[i][j]:
                que = deque()
                que.append((i,j))
                check[i][j] = False
                ans += 1
                while que:
                    y,x = que.popleft()
                    for k in range(8):
                        ny,nx = y+dy[k],x+dx[k]
                        if 0 <= ny < h and 0 <= nx < w:
                            if check[ny][nx]:
                                if c[ny][nx]:
                                    que.append((ny,nx))
                                    check[ny][nx] = False
    print(ans)

Python: def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x) 
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)

Python: dy = (-1,-1,0,1,1,1,0,-1)
dx = (0,1,1,1,0,-1,-1,-1)

def main():
    while True:
        w,h = map(int,input().split())
        if h == 0:
            return
        grid = [list(map(int,input().split())) for _ in range(h)]
        visited = [[0] * w for _ in range(h)]
        ans = 0
        for i in range(h):
            for j in range(w):
                if grid[i][j] == 1 and visited[i][j] == 0:
                    ans += 1
                    stack = [(i,j)]
                    visited[i][j] = 1
                    while stack:
                        p,q = stack.pop()
                        for k in range(8):
                            y = p + dy[k]
                            x = q + dx[k]
                            if y < 0 or y >= h:
                                continue
                            if x < 0 or x >= w:
                                continue
                            if grid[y][x] == 1 and visited[y][x] == 0:
                                stack.append((y,x))
                                visited[y][x] = 1
        print(ans)
if __name__ == '__main__':
    main()

Python: def main():
    W, H = (int(i) for i in input().split())
    while H != 0 or W != 0:
        c = [[int(i) for i in input().split()] for j in range(H)]
        seen = [[False for i in range(W)] for j in range(H)]
        def dfs(G,h,w,seen,H,W):
            todo = []
            hws = ((1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1))
            seen[h][w] = True
            todo.append((h,w))
            # todoが非空の間繰り返し
            while todo:
                h, w = todo[-1]
                for dh,dw in hws:
                    next_h = h + dh
                    next_w = w + dw
                    if not ((0 <= next_h < H) and (0 <= next_w < W)): continue
                    if G[next_h][next_w] == 0: continue
                    if seen[next_h][next_w]: continue
                    seen[next_h][next_w] = True
                    todo.append((next_h,next_w))
                    break
                else:
                    todo.pop()
        ans = 0
        for i in range(H):
            for j in range(W):
                if c[i][j] == 0 or seen[i][j]: continue
                dfs(c,i,j,seen,H,W)
                ans += 1
        print(ans)
        W, H = (int(i) for i in input().split())

if __name__ == '__main__':
    main()

Python: anslist = []
def sarch(i, j, visited):

    if 0 < i:
        if visited[i - 1][j] == 0:
            visited[i - 1][j] = 1
            sarch(i - 1, j, visited)
        if 0 < j:
            if visited[i - 1][j - 1] == 0:
                visited[i - 1][j - 1] = 1
                sarch(i - 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i - 1][j + 1] == 0:
                visited[i - 1][j + 1] = 1
                sarch(i - 1, j + 1, visited)

    if i < len(visited) - 1:
        if visited[i + 1][j] == 0:
            visited[i + 1][j] = 1
            sarch(i + 1, j, visited)
        if 0 < j:
            if visited[i + 1][j - 1] == 0:
                visited[i + 1][j - 1] = 1
                sarch(i + 1, j - 1, visited)
        if j < len(visited[0]) - 1:
            if visited[i + 1][j + 1] == 0:
                visited[i + 1][j + 1] = 1
                sarch(i + 1, j + 1, visited)

    if 0 < j:
        if visited[i][j - 1] == 0:
            visited[i][j - 1] = 1
            sarch(i, j - 1, visited)
    if j < len(visited[0]) - 1:
        if visited[i][j + 1] == 0:
            visited[i][j + 1] = 1
            sarch(i, j + 1, visited)
    return visited


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    grid = [list(map(int, input().split())) for j in range(h)]
    visited = [[0 for i in range(w)] for j in range(h)]
    starti = 0
    startj = 0
    for i in range(h):
        for j in range(w):
            if grid[i][j] == 0:
                visited[i][j] = -1
            else:
                visited[i][j] = 0

    i = 0
    j = 0
    cnt = 0
    ans = 0
    #print(visited)
    while True:
        if i >= h:
            i = 0
            j = 0
        while True:
            cnt += 1
            if j >= w:
                j = 0
                break
            #print(visited[i][j], 100100)
            if visited[i][j] == 0:
                ans += 1
                visited[i][j] = 1
                visited = sarch(i, j, visited)
                cnt = 0
            j += 1
        i += 1
        #print(cnt, visited)
        if cnt > w * h:
            #print(10)
            break
    anslist.append(ans)

for i in anslist:
    print(i)