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: # python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve(w, h):
    mat = [[0]*(w+2) for _ in range(h+2)]
    for j in range(h):
        l = list(map(int, input().split()))
        for i in range(w):
            mat[j+1][i+1] = l[i]
    visited = [[False]*(w+2) for _ in range(h+2)]

    ans = 0
    for i in range(h+2):
        for j in range(w+2):
            if visited[i][j]:
                continue
            else:
                if mat[i][j] == 1:
                    visited[i][j] = True
                    ans += 1
                    Q = [[i, j]]
                    ran = [1, 0, -1]
                    while Q:
                        x, y = Q.pop(-1)
                        for dx in ran:
                            for dy in ran:
                                if mat[x+dx][y+dy] == 1 and not visited[x+dx][y+dy]:
                                    visited[x+dx][y+dy] = True
                                    Q.append([x+dx, y+dy])
    return ans


a = []
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    else:
        a.append(solve(w, h))
print("\n".join(map(str, a)))

Python: if __name__ == '__main__':
    while 1:
        w,h = list(map(int,input().strip().split()))
        if w == h ==0:break
        imap = [list(map(int,input().strip().split())) for _ in range(h)]
        visited = []
        count = 0
        for j in range(h):
            for i in range(w):
                stack =[]
                if not (j,i) in visited and imap[j][i] == 1:
                    count +=1
                    stack.append((j,i))
                    visited.append(((j,i)))
                while len(stack) !=0:
                    y,x = stack.pop()
                    if y>0 and imap[y-1][x] == 1 and not (y-1,x) in visited:
                        visited.append((y-1,x))
                        stack.append((y-1,x))
                    if x>0 and imap[y][x-1] == 1 and not (y,x-1) in visited:
                        visited.append(((y,x-1)))
                        stack.append((y,x-1))
                    if y>0 and x>0 and imap[y-1][x-1] == 1 and not (y-1,x-1) in visited:
                        visited.append((y-1,x-1))
                        stack.append((y-1,x-1))
                    if y>0 and x<w-1 and imap[y-1][x+1] ==1 and not (y-1, x+1) in visited:
                        visited.append((y-1, x+1))
                        stack.append((y-1, x+1))
                    if y <h-1 and x>0 and imap[y+1][x-1] ==1 and not (y+1,x-1) in visited:
                        visited.append((y+1,x-1))
                        stack.append((y+1,x-1))
                    if y < h-1 and x < w-1 and imap[y+1][x+1] ==1and not (y+1,x+1) in visited:
                        visited.append((y+1,x+1))
                        stack.append((y+1,x+1))
                    if y < h-1 and imap[y+1][x]== 1 and not (y+1,x) in visited:
                        visited.append((y+1,x))
                        stack.append((y+1,x))
                    if x < w-1 and imap[y][x+1] == 1 and not (y,x+1) in visited:
                        visited.append((y,x+1))
                        stack.append((y,x+1))
        print(count)

Python: # -*- coding:utf-8 -*-
def solve():
    import sys
    sys.setrecursionlimit(100*100)

    anss = []
    while True:
        W, H = list(map(int, input().split()))

        if W==0 and H==0:
            break

        grid = [[] for _ in range(H)]
        for h in range(H):
            Cs = list(map(int, input().split()))
            grid[h] = Cs

        visited = [[0 for _ in range(W)] for __ in range(H)]  # 探索済みか？

        def dfs(h, w, depth):
            if w<0 or h<0 or w>=W or h>=H:
                return 0

            if visited[h][w] == 1:
                return 0
            visited[h][w] = 1

            if grid[h][w] == 0:
                return 0

            ret = 0
            for hh in range(h-1, h+2):
                for ww in range(w-1, w+2):
                    ret += dfs(hh, ww, depth+1)

            return ret+depth

        ans = 0
        for h in range(H):
            for w in range(W):
                if dfs(h, w, grid[h][w]) != 0:
                    ans += 1

        anss.append(ans)

    for ans in anss:
        print(ans)


if __name__ == "__main__":
    solve()

Python: def dfs(island, r, c):
    stack = [(r, c)]
    ans = 0
    while stack:
        r, c = stack.pop()
        if r < 0 or r >= len(island) or c < 0 or c >= len(island[r]):
            continue
        if island[r][c] == 0:
            continue
        ans += 1
        island[r][c] = 0
        for rr in range(-1, 2):
            for cc in range(-1, 2):
                if rr == 0 and cc == 0:
                    continue
                stack.append((r+rr, c+cc))
    return ans


def solve(w, h):
    island = []
    for _ in range(h):
        island.append(list(map(int, input().split())))
    cnt = 0
    for r in range(h):
        for c in range(w):
           if dfs(island, r, c) > 0:
               cnt += 1
    print(cnt)

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