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.
The problem can be solved by counting the servers in each row and column. If a server's row or column has more than one server, it can communicate with another server. Therefore, the solution consists of counting the number of servers in each row and column and then checking whether each server can communicate.
This solution uses a two-pass approach. The first pass populates the row_count and col_count arrays, which store the count of servers in each row and column respectively. The second pass checks for each server if it can communicate with any other server and increments the count accordingly.
Time Complexity: O(m * n) where m is the number of rows and n is the number of columns.
Space Complexity: O(m + n) for storing row and column counts.
The problem can alternatively be approached by directly traversing the matrix once, using auxiliary data structures to maintain counts of rows and columns. This helps in determining if a server at any particular cell can communicate by inspecting these counts directly.
This approach uses Python's defaultdict to dynamically store and access the count of servers in rows and columns, allowing efficient traversal and checking within the grid.
Time Complexity: O(m * n) for traversal.
Space Complexity: O(m + n) for auxiliary storage of counts.
We can count the number of servers in each row and each column, then traverse each server. If the number of servers in the current server's row or column exceeds 1, it means the current server meets the condition, and we increment the result by 1.
After the traversal, we return the result.
The time complexity is O(m times n), and the space complexity is O(m + n). Where m and n are the number of rows and columns in the matrix, respectively.
| Approach | Complexity |
|---|---|
| Row and Column Count | Time Complexity: O(m * n) where m is the number of rows and n is the number of columns. |
| Matrix Traversal | Time Complexity: O(m * n) for traversal. |
| Counting | — |
| 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 |
Count Servers that Communicate - Leetcode 1267 - Python • NeetCodeIO • 10,050 views views
Watch 9 more video solutions →Practice Count Servers that Communicate with our built-in code editor and test cases.
Practice on FleetCode