solved Task 2 for squared matrix