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
sys.setrecursionlimit(10000)

def get_two_int():
    two_int = input().split()
    for i in range(2):
        two_int[i] = int(two_int[i])
    return two_int

def get_data_list():
    data_list = input().split()
    for i, v in enumerate(data_list):
        data_list[i] = int(v)
    # print("data_list", data_list) # debug
    return data_list


def make_map(width, height):
    map_list = [[0] * (width + 2) for x in range(height + 2)] # まわりを0で囲む
    # print("map_list", map_list) # debug pprint?
    for h in range(1, height + 1):
        line_list = get_data_list()
        for w in range(1, width + 1):
            map_list[h][w] = line_list[w-1] # 汚い
    #print("map_list", map_list) # debug
    return map_list

def count_island(width, height, map_list):
    count = 0
    checked_set = set()
    for h in range(1, height + 1):
        for w in range(1, width + 1):
            if map_list[h][w] == 1 and ((h, w) not in checked_set):
                map_list, checked_set = check_around(h, w, map_list, checked_set)
                count += 1
    # print("checked_set", checked_set) # debug
    return count

def check_around(h, w, map_list, checked_set):
    # print((h, w)) #debug
    checked_set.add((h, w))
    # print("map_list", map_list) # debug
    for i in range(h - 1, h + 2): # まわりを全てチェック、大枠のまわりを0で余分に囲っているので無問題
        for j in range(w - 1, w + 2):
            if map_list[i][j] == 1 and (i, j) not in checked_set:
                    map_list, checked_set = check_around(i, j, map_list, checked_set)
    return map_list, checked_set


if __name__ == "__main__":
    while True:
        width, height = get_two_int()
        if width == 0:
            break
        # print("width", width) # debug
        # print("height", height) # debug
        map_list = make_map(width, height)
        # print("map_list", map_list) # debug 
        count = count_island(width, height, map_list)
        print(count)

Python: import sys

sys.setrecursionlimit(20000)

def fill(m, h, w, x, y):
    m[y][x] = 0
    for i in range(-1, 2):
        for j in range(-1, 2):
            nx = x + i
            ny = y + j
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue
            if m[ny][nx] == 0:
                continue
            fill(m, h, w, nx, ny)

def solve(h, w):
    m = []
    for j in range(h):
        line = list(map(int, input().rstrip().split(' ')))
        m.append(line)
    

    count = 0
    for y in range(h):
        for x in range(w):
            if m[y][x] != 0:
                count += 1
                fill(m, h, w, x, y)

    print(count)

while True:
    w, h = map(int, input().rstrip().split(' '))
    if h == 0 or w == 0:
        break

    solve(h, w)

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()
                    for dx in [-1,0,1]:
                        for dy in [-1,0,1]:
                            if  0<= x + dx < w and 0<= y + dy <h and not (y+dy,x+dx)in visited:
                                if imap[y + dy][x + dx] == 1:
                                    stack.append((y+dy,x+dx))
                                    visited.append(((y+dy,x+dx)))
        print(count)

Python: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    if 0<=x<w and 0<=y<h and a[y][x]=='1':
        a[y][x]='0'
        for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:f(x+dx,y+dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[list(input().split()) for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

import sys
sys.setrecursionlimit(10000)


def dfs(x: int, y: int):
    """dfsで1つの地続きの島を探索
    Parameters
    ----------
        x, y : 探索対称点のxy座標
    """
    seen[x][y] = True
    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if (0 <= nx < H
                and 0 <= ny < W
                and seen[nx][ny] is False
                and chart[nx][ny] == 1):
            dfs(nx, ny)


while True:
    try:
        W, H = map(int, input().split())
        if W == 0:
            break
        else:
            chart = []
            for _ in range(H):
                chart.append(list(map(int, input().split())))
            seen = [[False] * W for _ in range(H)]
            dx = [1, 0, -1, 0, 1, 1, -1, -1]
            dy = [0, 1, 0, -1, 1, -1, -1, 1]
            ans = 0
            for i in range(H):
                for j in range(W):
                    # seen=Trueのマスは既に島としてカウントされた故それ以外を探索
                    if seen[i][j] is False and chart[i][j] == 1:
                        dfs(i, j)
                        # dfsで島1つを探索終了した故カウントをインクリメント
                        ans += 1
            print(ans)
    except ValueError:
        break

Python: 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 -1 and 0 <= ny <= h - 1:
            if c[ny][nx] == '1':
                erase(nx, ny)

while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [[i for i in 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: # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)


def search(n, m, tile, N, M):
    for dy, dx in zip([-1, -1, -1, 0, 0, 1, 1, 1], [-1, 0, 1, -1, 1, -1, 0, 1]):
        if 0 <= dy + n < N and 0 <= dx + m < M:
            if tile[dy + n][dx + m] == '1':
                tile[dy + n][dx + m] = '2'
                search(dy + n, dx + m, tile, N, M)


while True:
    M, N = map(int, input().split())
    if M == 0 and N == 0:
        break
    tile = [input().split() for n in range(N)]
    ans = 0
    for n in range(N):
        for m in range(M):
            if tile[n][m] == '1':
                tile[n][m] = '2'
                search(n, m, tile, N, M)
                ans += 1
    print(ans)