Watch 10 video solutions for Count Servers that Communicate, a medium level problem involving Array, Depth-First Search, Breadth-First Search. This walkthrough by NeetCodeIO has 10,050 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
Return the number of servers that communicate with any other server.
Example 1:

Input: grid = [[1,0],[0,1]] Output: 0 Explanation: No servers can communicate with others.
Example 2:

Input: grid = [[1,0],[1,1]] Output: 3 Explanation: All three servers can communicate with at least one other server.
Example 3:

Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] Output: 4 Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
Constraints:
m == grid.lengthn == grid[i].length1 <= m <= 2501 <= n <= 250grid[i][j] == 0 or 1Problem Overview: You receive a binary m x n grid where 1 represents a server and 0 represents an empty cell. A server can communicate if there is at least one other server in the same row or column. The task is to count how many servers can communicate with another server.
Approach 1: Matrix Traversal (Brute Force) (Time: O(mn(m+n)), Space: O(1))
Iterate through every cell in the grid. When you encounter a server (grid[i][j] == 1), scan the entire row i and column j to check if another server exists. If at least one additional server appears in either direction, include the current server in the count. This method relies purely on repeated matrix traversal and avoids extra memory. The drawback is performance: each server triggers two additional scans, which makes the complexity grow to O(mn(m+n)). It works for small grids but becomes inefficient when the matrix size increases. The approach mainly reinforces basic matrix traversal techniques.
Approach 2: Row and Column Count (Time: O(mn), Space: O(m+n))
The key observation: a server communicates if its row or column contains more than one server. Instead of repeatedly scanning rows and columns, maintain two arrays: rowCount[m] and colCount[n]. First pass: iterate through the grid and increment counters whenever a server appears. Second pass: revisit each server and check if rowCount[i] > 1 or colCount[j] > 1. If true, that server can communicate. This reduces redundant work because row and column totals are computed once. The algorithm runs in linear time relative to the grid size. It is a common optimization pattern when solving array and matrix counting problems.
Recommended for interviews: The row and column counting approach is the expected solution. Interviewers want to see the transition from repeated scanning to precomputed counts. Starting with brute force demonstrates problem understanding, while the optimized two-pass counting solution shows awareness of time complexity and efficient data aggregation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Matrix Traversal (Brute Force) | O(mn(m+n)) | O(1) | Useful for understanding the communication rule or when grid sizes are very small |
| Row and Column Count | O(mn) | O(m+n) | Best general solution; avoids repeated scans by precomputing row and column server counts |