01
Overview

Eight-neighbor counting with safe bounds.

Look at all eight surrounding cells — up, down, left, right, and the four diagonals — without ever stepping outside the grid.

Many grid problems care about the eight neighbors of a cell — the four orthogonal plus the four diagonal neighbors. This is the standard for image-processing kernels, game-of-life-style simulations, and minesweeper-like games.

The challenge is that edge and corner cells have fewer than eight neighbors. A clean solution uses delta arrays plus a bounds check.

02
Core Concept

Delta arrays + bounds check.

Loop over eight offsets; skip any out-of-bounds combinations.

Direction offsets

int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] dc = {-1,  0,  1, -1, 1, -1, 0, 1};

Each pair (dr[d], dc[d]) represents one of the eight directions. Together they cover all neighbors and exclude the cell itself ((0, 0) is skipped).

Counting matching neighbors

public static int countNeighbors(int[][] grid, int r, int c, int target) {
    int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1};
    int[] dc = {-1,  0,  1, -1, 1, -1, 0, 1};

    int count = 0;
    for (int d = 0; d < 8; d++) {
        int nr = r + dr[d];
        int nc = c + dc[d];
        if (nr >= 0 && nr < grid.length
         && nc >= 0 && nc < grid[0].length
         && grid[nr][nc] == target) {
            count++;
        }
    }
    return count;
}

Why delta arrays?

You could write eight separate if statements for each direction, but that's repetitive and error-prone. The delta-array pattern is the standard idiom for AP-style grid problems and beyond.

Excluding the center

The (0, 0) offset would mean "the cell itself" — and that's not a neighbor. By omitting it from the delta arrays, you avoid counting it.

03
Key Vocabulary

Eight-neighbor vocabulary.

The vocabulary of grid-game algorithms.

Vocabulary
  • Eight neighbors — the cells surrounding a center cell on all sides and diagonals.
  • Orthogonal neighbor — up, down, left, right.
  • Diagonal neighbor — NW, NE, SW, SE.
  • Delta array — array of offsets describing directions.
  • Bounds check — verifying the candidate neighbor is in the grid.
  • Center cell — the cell whose neighbors are being counted.
04
AP Exam Focus

How eight-neighbor problems are tested.

Corner cell behavior and including the center are the top traps.

Exam Focus
  • Corner cells have 3 neighbors; edge non-corner cells have 5; interior cells have 8.
  • Center exclusion. Make sure the cell itself is not counted.
  • Bounds first, value second. Always check coordinates before accessing.
05
Worked Example

Count 1-neighbors of a corner cell.

Verify that bounds checks eliminate invalid directions.

Worked Example AP-style reasoning

Problem. Count cells with value 1 around (0, 0) in:

{{0, 1, 0},
 {1, 1, 0},
 {0, 0, 0}}

Trace each direction:

  • NW (-1, -1) → out of bounds. Skip.
  • N (-1, 0) → out of bounds. Skip.
  • NE (-1, 1) → out of bounds. Skip.
  • W (0, -1) → out of bounds. Skip.
  • E (0, 1) → grid[0][1] = 1. Count!
  • SW (1, -1) → out of bounds. Skip.
  • S (1, 0) → grid[1][0] = 1. Count!
  • SE (1, 1) → grid[1][1] = 1. Count!

Answer. 3 neighbors with value 1.

06
Common Mistakes

Eight-neighbor pitfalls.

All result in wrong counts.

Watch Out
  • Including the center cell — adds 1 to every count.
  • Skipping the bounds check. Crashes on edges.
  • Wrong delta array lengths. dr and dc must be parallel.
  • Mixing orthogonal-only with diagonal. Some problems want 4, others want 8 — read carefully.
07
Reference Table

Neighbor counts by position.

A sanity check for your code's output.

Eight Neighbors Reference

AP Quick Reference
Cell position Number of neighbors AP Exam Tip
Interior 8 All directions valid.
Edge (non-corner) 5 One side is out of bounds.
Corner 3 Two sides are out of bounds.
1x1 grid 0 No neighbors exist.
08
Practice Tip

How to master eight-neighbor counting.

Verify on all three cell types: interior, edge, corner.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for each candidate cell, list which of the 8 directions are in bounds before counting.
  • Java Lab — implement countNeighbors and test on every position of a 3x3 grid. Confirm counts of 3, 5, 8 as expected.
  • FRQ Practice — for any "surrounded by" problem, use the delta-array pattern with a bounds check.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

Topic Quiz 40 min 40 marks Pending

AP CSA Topic Practice: Eight-Neighbor Counting with Bounds

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 05 Topic 07 Eight-Neighbor Counting with Bounds FRQ Practice

AP-style topic practice assessment

Start