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: # AOJ 1160: How Many Islands?
# Python3 2018.7.15 bal4u

# UNION-FIND library
class UnionSet:
	def __init__(self, nmax):
		self.size = [1]*nmax
		self.id = [i for i in range(nmax+1)]
	def root(self, i):
		while i != self.id[i]:
			self.id[i] = self.id[self.id[i]]
			i = self.id[i]
		return i
	def connected(self, p, q): return self.root(p) == self.root(q)
	def unite(self, p, q):
		i, j = self.root(p), self.root(q)
		if i == j: return
		if self.size[i] < self.size[j]:
			self.id[i] = j
			self.size[j] += self.size[i]
		else:
			self.id[j] = i
			self.size[i] += self.size[j]
# UNION-FIND library

def id(r, c):
	global w
	return r*w+c

mv = [[-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: break
	arr = [[] for r in range(h)]
	u = UnionSet(w*h)
	for r in range(h): arr[r] = list(map(int, input().split()))
	for r in range(h):
		for c in range(w):
			if not arr[r][c]: continue
			for i in range(8):
				r2, c2 = r + mv[i][0], c + mv[i][1]
				if r2 < 0 or r2 >= h or c2 < 0 or c2 >= w or not arr[r2][c2]: continue
				u.unite(id(r, c), id(r2, c2))
	ans = 0
	for r in range(h):
		for c in range(w):
			if arr[r][c] and u.root(id(r, c)) == id(r, c): ans += 1
	print(ans)

Python: from itertools import product
while True:
    w, h = map(int, input().split())
    if w == 0:
        break
    a = [[0]*(w+2)] + [[0]+list(map(int, input().split()))+[0] for _ in [0]*h] + [[0]*(w+2)]
    visited = [[0]*(w+2) for _ in [0]*(h+2)]
    ans = 0
    for y in range(1, h+1):
        for x in range(1, w+1):
            if visited[y][x] or not a[y][x]:
                continue
            ans += 1
            visited[y][x] = 1
            q = [(x, y)]
            pop, append = q.pop, q.append
            while q:
                _x, _y = pop()
                for dx, dy in product(range(-1, 2), range(-1, 2)):
                    nx, ny = _x+dx, _y+dy
                    if not visited[ny][nx] and a[ny][nx]:
                        visited[ny][nx] = 1
                        append((nx, ny))
    print(ans)

Python: 
# 深さ優先探索
def dfs(sy, sx, visited, C):
    cango = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]]
    stack = [[sy, sx]]
    if visited[sy][sx] == 1:#きたことあるならスルー
      return 0
    visited[sy][sx] = 1
    if C[sy][sx] == 0:#もし海ならスルー
      return 0
    while stack:
      y, x = stack[-1]
      stack.pop()
      for i, j in cango:
        if (0 <= y + i < H) and (0 <= x + j < W):
          if (visited[y + i][x + j] == 0) and (C[y + i][x + j] == 1):
            visited[y + i][x + j] = 1
            stack.append([y + i, x + j])
    return 1

def dfs2(W, H, C1):
  ans = 0  
  visited = [[0] * W for i in range(H)]
  C = C1
  for i in range(H):
    for j in range(W):
      ans += dfs(i, j, visited, C)
    
  print(ans)

p = 0
while (p == 0):
#from collecitons import deque
  W, H = map(int, input().split())
  if (W, H) == (0, 0):
    p == 1
    quit()
  C1 = [0] * H
  for i in range(H):
    C1[i] = list(map(int, input().split()))
  dfs2(W, H, C1)

Python: import sys
sys.setrecursionlimit(10**7)
vec = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)]
def dfs(x,y,visited,field):
        
     
        visited[x][y] = 1
        for nx,ny in vec:
            tx = x + nx
            ty = y+ny
            if tx < 0 or ty < 0 or tx > h-1 or ty > w-1:continue
            if visited[tx][ty]:continue
            if field[tx][ty] == 0:continue
            dfs(tx,ty,visited,field)
            
        return
while True:    
    w,h = map(int,input().split())
    if w == 0 and h == 0:break
    field = [list(map(int,input().split()))for i in range(h)]
    visited = [[0]*w for i in range(h)]
    ans = 0
    
    for i in range(h):
        for j in range(w):
            if field[i][j] == 1 and visited[i][j] == 0:
                dfs(i,j,visited,field)
                ans +=1
    
    print(ans)

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



def tansaku(i,j):
	for di in range(-1,2):
		for dj in range(-1,2):
			if 0<=i+di<h and 0<=j+dj<w:
					if visited[i+di][j+dj]:
						continue
					else:
						visited[i][j]=1
						if c[i][j]==0:continue
						tansaku(i+di,j+dj)
			else:continue
			
			
	return

			
def dfs(i,j):
	global count
	if visited[i][j]:return
	visited[i][j]=1
	# print(visited)
	if c[i][j]==0:return
	count+=1
	tansaku(i,j)

while True:
	# print('スタート')
	w,h =list(map(int,input().split()))
	if w==0 and h==0:break
	c=[list(map(int,input().split())) for i in range(h)]
	visited=[[0]*w for i in range(h)]
	count=0
	for s in range(w):
		for t in range(h):
			dfs(t,s)

	print(count)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(2):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

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

Python: import sys
sys.setrecursionlimit(100000)


def dfs(x,graph,seen,nxt):
    seen[x]=1
    if x in nxt:
        nxt.remove(x)
    nxt+=[y for y in graph[x] if not seen[y] and y not in nxt]
    while nxt :
        dfs(nxt.pop(-1),graph,seen,nxt)


def island_count(w,h):
    lst=[list(map(int,input().split())) for _ in range(h)]
    graph={}
    for i in range(h):
        for j in range(w):
            if not lst[i][j] : continue
            graph[(i,j)]=[]
            for k in range(-1,2):
                for l in range(-1,2):
                    if k==0 and l==0 : continue
                    if i+k<0 or j+l<0 : continue
                    try:
                        if lst[i+k][j+l]:
                            graph[(i,j)].append((i+k,j+l))
                    except IndexError:
                        continue
    #print(graph)
    seen={x:0 for x in graph}
    sm=0
    nxt=[]
    for x in seen:
        if seen[x] : continue
        if not seen[x]:
            dfs(x,graph,seen,nxt)
            sm+=1

    print(sm)






while 1 :
    w,h=map(int,input().split())
    if (w,h)==(0,0) : break
    island_count(w,h)