Given the position of the king on an **8 X 8 chessboard**, the task is to count the total number of squares that can be visited by the king in **m** moves. The position of the king is denoted using row and column number.

**Note:** The square which is currently acquired by the king is already visited and will be counted in the result.

**Examples:**

Input:r = 4, c = 4, m = 1

Output:9

Input:r = 4, c = 4, m = 2

Output:25

**Approach:** A king can move one square in any direction (i.e horizontally, vertically and diagonally). So, in one move king can visit its adjacent squares.

So, A square which is within **m** units distance (Considering 1 Square as 1 unit distance) from the king’s current position can be visited in **m** moves.

- For all squares of the chessboard, check if a particular square is at
**m**unit distance away or less from King’s current position. - Increment count, if step 1 is true.
- Print the
**count**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of squares ` `// that can be visited by king in m moves ` `int` `countSquares(` `int` `r, ` `int` `c, ` `int` `m) ` `{ ` ` ` ` ` `// To store the count of squares ` ` ` `int` `squares = 0; ` ` ` ` ` `// Check all squares of ` ` ` `// the chessboard ` ` ` `for` `(` `int` `i = 1; i <= 8; i++) { ` ` ` `for` `(` `int` `j = 1; j <= 8; j++) { ` ` ` ` ` `// Check if square (i, j) is ` ` ` `// at a distance <= m units ` ` ` `// from king's current position ` ` ` `if` `(max(` `abs` `(i - r), ` `abs` `(j - c)) <= m) ` ` ` `squares++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return count of squares ` ` ` `return` `squares; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `r = 4, c = 4, m = 1; ` ` ` ` ` `cout << countSquares(r, c, m) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the count of squares ` ` ` `// that can be visited by king in m moves ` ` ` `static` `int` `countSquares(` `int` `r, ` `int` `c, ` `int` `m) ` ` ` `{ ` ` ` `// To store the count of squares ` ` ` `int` `squares = ` `0` `; ` ` ` ` ` `// Check all squares of ` ` ` `// the chessboard ` ` ` `for` `(` `int` `i = ` `1` `; i <= ` `8` `; i++) { ` ` ` `for` `(` `int` `j = ` `1` `; j <= ` `8` `; j++) { ` ` ` ` ` `// Check if square (i, j) is ` ` ` `// at a distance <= m units ` ` ` `// from king's current position ` ` ` `if` `(Math.max(Math.abs(i - r), Math.abs(j - c)) <= m) ` ` ` `squares++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return count of squares ` ` ` `return` `squares; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `r = ` `4` `, c = ` `4` `, m = ` `1` `; ` ` ` `System.out.print(countSquares(r, c, m)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to return the count of squares ` ` ` `// that can be visited by king in m moves ` ` ` `static` `int` `countSquares(` `int` `r, ` `int` `c, ` `int` `m) ` ` ` `{ ` ` ` `// To store the count of squares ` ` ` `int` `squares = 0; ` ` ` ` ` `// Check all squares of ` ` ` `// the chessboard ` ` ` `for` `(` `int` `i = 1; i <= 8; i++) { ` ` ` `for` `(` `int` `j = 1; j <= 8; j++) { ` ` ` ` ` `// Check if square (i, j) is ` ` ` `// at a distance <= m units ` ` ` `// from king's current position ` ` ` `if` `(Math.Max(Math.Abs(i - r), Math.Abs(j - c)) <= m) ` ` ` `squares++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return count of squares ` ` ` `return` `squares; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `r = 4, c = 4, m = 1; ` ` ` `Console.Write(countSquares(r, c, m)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Function to return the count of squares ` `# that can be visited by king in m moves ` `def` `countSquares(r, c, m): ` ` ` ` ` `# To store the count of squares ` ` ` `squares ` `=` `0` ` ` ` ` `# Check all squares of ` ` ` `# the chessboard ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `9` `): ` ` ` `for` `j ` `in` `range` `(` `1` `, ` `9` `): ` ` ` ` ` `# Check if square (i, j) is ` ` ` `# at a distance <= m units ` ` ` `# from king's current position ` ` ` `if` `(` `max` `(` `abs` `(i ` `-` `r), ` `abs` `(j ` `-` `c)) <` `=` `m): ` ` ` `squares ` `=` `squares ` `+` `1` ` ` ` ` `# Return count of squares ` ` ` `return` `squares ` ` ` `# Driver code ` `r ` `=` `4` `c ` `=` `4` `m ` `=` `1` ` ` `print` `(countSquares(r, c, m)); ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count of squares ` `// that can be visited by king in m moves ` `function` `countSquares(` `$r` `, ` `$c` `, ` `$m` `) ` `{ ` ` ` ` ` `// To store the count of squares ` ` ` `$squares` `= 0; ` ` ` ` ` `// Check all squares of ` ` ` `// the chessboard ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= 8; ` `$i` `++) ` ` ` `{ ` ` ` `for` `(` `$j` `= 1; ` `$j` `<= 8; ` `$j` `++) ` ` ` `{ ` ` ` ` ` `// Check if square (i, j) is ` ` ` `// at a distance <= m units ` ` ` `// from king's current position ` ` ` `if` `(max(` `abs` `(` `$i` `- ` `$r` `), ` ` ` `abs` `(` `$j` `- ` `$c` `)) <= ` `$m` `) ` ` ` `$squares` `++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return count of squares ` ` ` `return` `$squares` `; ` `} ` ` ` `// Driver code ` `$r` `= 4; ` `$c` `= 4; ` `$m` `= 1; ` ` ` `echo` `countSquares(` `$r` `, ` `$c` `, ` `$m` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

9

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:

- Total position where king can reach on a chessboard in exactly M moves
- Minimum number of moves required to reach the destination by the king in a chess board
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Check if any King is unsafe on the Chessboard or not
- Find all the queens attacking the king in a chessboard
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard
- Minimum moves to reach target on a infinite line | Set 2
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Minimum number of moves to reach N starting from (1, 1)
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Maximum bishops that can be placed on N*N chessboard
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Check if a Queen can attack a given cell on chessboard
- Maximum non-attacking Knights that can be placed on an N*M Chessboard
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Total ways of choosing X men and Y women from a total of M men and W women

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.