C # implementation of the approach
using System ; using System . Collections . Generic ; class GFG { static int [ ] m ; static int [ , ] a ; static int [ ] x ; static List < int > [ ] g ; static int ans = 0 ;