// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;

public class HelloWorld
{
    static int ReadPositiveInt(string message)
    {
        int sk;
        bool cikls = true;
        do{
            Console.Write(message);
            string num = Console.ReadLine();
            if (int.TryParse(num, out sk) && sk >0){
                cikls = false;
            }
        }while(cikls);
        return sk;
    }
    static int[,] GenerateMatrix(int rindas, int kolonnas)
    {
        Random rand = new Random();
        int[,] matrix = new int[rindas,kolonnas];
        
        for(int i = 0; i< matrix.GetLength(0);i++)
            {
                for(int j = 0; j< matrix.GetLength(1);j++)
                {
                    matrix[i, j] = rand.Next(-100, 101);
                }
            }
        return matrix;
    }
    static void PrintMatrix(int[,] matrix)
    {
        int rindas = matrix.GetLength(0);
        int kolonnas = matrix.GetLength(1);
        Console.WriteLine("Matrica");
        
        for(int i = 0; i< rindas;i++)
            {
                for(int j = 0; j< kolonnas;j++)
                {
                    Console.Write("+-------");
                }
            
            Console.WriteLine("+");
            for(int j = 0; j< kolonnas;j++)
                {
                    Console.Write($"|\t{matrix[i,j]}\t");
                }
            Console.WriteLine("|");
            }
        for(int j = 0; j< kolonnas;j++)
                {
                    Console.Write("+-------");
                }
                Console.WriteLine("+");
    }
    static int FindMax(int[,] matrix)
    {
        int max = 0;
        for(int i = 0; i<matrix.GetLength(0); i++)
        {
            for(int j = 0; j<matrix.GetLength(1); j++)
            {
                if(max < matrix[i,j])
                {
                    max = matrix[i,j];
                }
            }
        }
        return max;
    }
    static int FindMin(int[,] matrix)
    {
        int min = 0;
        for(int i = 0; i<matrix.GetLength(0); i++)
        {
            for(int j = 0; j<matrix.GetLength(1); j++)
            {
                if(min > matrix[i,j])
                {
                    min = matrix[i,j];
                }
            }
        }
        return min;
    }
    static int[] ColumnSums(int[,] matrix)
    {
        int rindas = matrix.GetLength(0);
        int kolonnas = matrix.GetLength(1);
        int[] kolonnuSumma = new int[kolonnas];
        
        for(int i = 0; i<rindas;i++)
        {
            for(int j = 0; j<kolonnas; j++)
            {
                kolonnuSumma[j] = kolonnuSumma[j] + matrix[i,j];
                
            }
        }
        
        return kolonnuSumma;
    }
    static int MainDiagonalSum(int[,] matrix)
    {
        int diagonalSumma = 0;
        
        int rindas = matrix.GetLength(0);
        int kolonnas = matrix.GetLength(1);
        
        for(int i = 0; i<rindas;i++)
        {
            for(int j = 0; j<kolonnas; j++)
            {
                if (i == j)
                    diagonalSumma += matrix[i,j];
            }
        }//https://www.geeksforgeeks.org/dsa/efficiently-compute-sums-of-diagonals-of-a-matrix/ 
        
        return diagonalSumma;
    }
    static int SecondaryDiagonalSum(int[,] matrix)
    {
        int SecondarydiagonalSumma = 0;
        
        int rindas = matrix.GetLength(0);
        int kolonnas = matrix.GetLength(1);
        
        for(int i = 0; i<rindas;i++)
        {
            for(int j = 0; j<kolonnas; j++)
            {
                if ((i + j) == (kolonnas - 1))
                    SecondarydiagonalSumma += matrix[i,j];
            }
        }//https://www.geeksforgeeks.org/dsa/efficiently-compute-sums-of-diagonals-of-a-matrix/ 
        
        return SecondarydiagonalSumma;
    }
    public static void Main(string[] args)
    {
        int rindas = ReadPositiveInt("ievadi rindas ");
        int kolonnas = ReadPositiveInt("ievadi kolonnas ");
        int[,] matrix = GenerateMatrix(rindas,kolonnas);
        
        PrintMatrix(matrix);
        int max = FindMax(matrix);
        Console.WriteLine($"max {max}");
        int min = FindMin(matrix);
        Console.WriteLine($"min {min}");
        int[] kolonnuSumma = ColumnSums(matrix);
        foreach(int s in kolonnuSumma)
        {
            Console.Write($"KolonnuSumma {s} ");
        }
        int diagonalSumma = MainDiagonalSum(matrix);
        Console.WriteLine($"Diagonal Summa {diagonalSumma}");
        int SecondarydiagonalSumma = SecondaryDiagonalSum(matrix);
        Console.WriteLine($"Secondary Diagonal Summa {SecondarydiagonalSumma}");
        /*Console.WriteLine("ievadi rindas");
        string rindas = Console.ReadLine();
        bool parbaude = int.TryParse(rindas, out int value);
        Console.WriteLine("ievadi kolonnas");
        string kolonnas = Console.ReadLine();
        bool parbaude2 = int.TryParse(kolonnas, out int value2);
        if (parbaude && parbaude2 == true)
        {
            int[,] matrix = new int[value,value2];
            Random rand = new Random();
            for(int i = 0; i< matrix.GetLength(0);i++)
            {
                for(int j = 0; j< matrix.GetLength(1);j++)
                {
                    matrix[i, j] = rand.Next(-100, 100);
                    Console.Write($"{matrix[i,j]}\t");
                }
                Console.WriteLine();
            }
        }
        */
        
    }
}