Given an **M × N** matrix. The task is to count the number of adjacent cells and calculate their sum.

Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally.

**Examples :**

Input :m = 2, n = 2

Output :12

Input :m = 3, n = 2

Output:22

See the below diagram where numbers written on it denotes number of adjacent squares.

**Approach:**

In a m X n grid there can be 3 cases:

- Corner cells touch 3 cells, and there are always 4 corner cells.
- Edge cells touch 5 cells, and there are always 2 * (m+n-4) edge cells.
- Interior cells touch 8 cells, and there are always (m-2) * (n-2) interior cells.

Therefore,

Sum = 3*4 + 5*2*(m+n-4) + 8*(m-2)*(n-2) = 8mn - 6m - 6n +4

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate the sum of all cells adjacent value ` `int` `sum(` `int` `m, ` `int` `n) ` `{ ` ` ` `return` `8 * m * n - 6 * m - 6 * n + 4; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `m = 3, n = 2; ` ` ` `cout << sum(m, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` ` ` `// function to calculate the sum ` ` ` `// of all cells adjacent value ` ` ` `static` `int` `sum(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `return` `8` `* m * n - ` `6` `* m - ` `6` `* n + ` `4` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `m = ` `3` `, n = ` `2` `; ` ` ` `System.out.println(sum(m, n)); ` ` ` `} ` `} ` ` ` `// This Code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# function to calculate the sum ` `# of all cells adjacent value ` `def` `summ(m, n): ` ` ` `return` `8` `*` `m ` `*` `n ` `-` `6` `*` `m ` `-` `6` `*` `n ` `+` `4` ` ` `# Driver Code ` `m ` `=` `3` `n ` `=` `2` `print` `(summ(m, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` ` ` `// function to calculate the sum ` ` ` `// of all cells adjacent value ` ` ` `static` `int` `sum(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `return` `8 * m * n - 6 * m - 6 * n + 4; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main (String []args) ` ` ` `{ ` ` ` `int` `m = 3, n = 2; ` ` ` `Console.WriteLine(sum(m, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by andrew1234 ` |

*chevron_right*

*filter_none*

**Output:**

22

**Time Complexity:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count Magic squares in a grid
- Find the number of p-sided squares in a grid with K blacks painted
- Find the number of squares inside the given square grid
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Minimum product in a grid of adjacent elements
- Minimum number of squares whose sum equals to given number n
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number N | set 2
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Count of primes in a given range that can be expressed as sum of perfect squares
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Find K numbers with sum equal to N and sum of their squares maximized
- Count number of squares in a rectangle
- Program to count number of distinct Squares and Cubes upto N
- Count number less than N which are product of perfect squares
- Count the total number of squares that can be visited by Bishop in one move

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.