Description

An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image. The filter replaces each cell with the average of surrounding cells. Given a 2D integer matrix img representing a grayscale image, return the image after applying the smoother on each cell.

Examples

Input:img = [[1,1,1],[1,0,1],[1,1,1]]
Output:[[0,0,0],[0,0,0],[0,0,0]]
Explanation:

All cells average to 0.

Input:img = [[1]]
Output:[1]
Explanation:

A 1x1 image has only one cell with no neighbors, so its smoothed value is itself: 1.

Input:img = [[100,200,100],[50,0,50],[100,200,100]]
Output:[[87,100,87],[112,112,112],[87,100,87]]
Explanation:

For each cell, the average of all valid neighboring cells (including itself) is calculated. The center cell (0) has 8 neighbors plus itself: (100+200+100+50+0+50+100+200+100)/9 = 900/9 = 100. Corner cells have fewer neighbors - top-left averages (100+200+50+0)/4 = 87.

Constraints

  • m == img.length
  • n == img[i].length
  • 1 ≤ m, n ≤ 200

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!