Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Count the islands

    Count the islands

    Mediumgraphdfsbfsmatrix

    A sea map: each row is a string of "1" (land) and "0" (water). An island is land cells connected horizontally or vertically (not diagonally). Return the number of islands.

    Example 1

    Input:  grid = ["11110","11010","11000","00000"]
    Output: 1

    Example 2

    Input:  grid = ["11000","11000","00100","00011"]
    Output: 3

    Constraints

    • 0 ≤ rows, columns ≤ 300
    • Each cell is '0' or '1'

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {string[]} grid
     * @return {int}
     */
    function numIslands(grid) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.