///P3 - 小綠綠的期中專題-數位影像分析

#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#define MAX 300

using namespace std;

struct pixel
{
    int R;
    int G;
    int B;
    int P;
};

struct pixel image[MAX][MAX];
int bright[90000];
int W,H;

int main()
{
    ///read image size
    cin >> W >> H;
    int N = W * H;
    int Size = N * 3;
    int sum = 0;
    int c = 0;

    ///read image RGB values and calculate the brightness
    for (int i=0; i<H; i++)
        for (int j=0; j<W; j++)
    {
        cin >> image[i][j].R >> image[i][j].G >> image[i][j].B;
        image[i][j].P = (image[i][j].R +image[i][j].G + image[i][j].B)/3;
        sum += image[i][j].P ;
        bright[c++]=image[i][j].P;
    }

    int Mean = sum / N;

    double sum_sd = 0;
    for (int i=0; i<H; i++)
        for (int j=0; j<W; j++)
    {
        sum_sd += pow(image[i][j].P - Mean,2);
    }
    double SD = sqrt(sum_sd / N);
    int Mid;

    sort(bright,bright+N);
    if (N%2==1)
        Mid = bright[N/2];
    else
        Mid = (bright[N/2]+bright[(N-1)/2]) / 2;


    /// show result
    cout << "SIZE = " << Size << " Bytes" << endl;
    cout << "MEAN = " << Mean << endl;
    cout << "SD = " << fixed << setprecision(2) << SD << endl;
    cout << "MID = " << Mid << endl;


    return 0;
}

/*
5 6
0 0 0 255 255 255 0 0 0 255 255 255 0 0 0
255 255 255 0 0 0 255 255 255 0 0 0 255 255 255
0 0 0 255 255 255 0 0 0 255 255 255 0 0 0
255 255 255 0 0 0 255 255 255 0 0 0 255 255 255
0 0 0 255 255 255 0 0 0 255 255 255 0 0 0
255 255 255 0 0 0 255 255 255 0 0 0 255 255 255
SIZE = 90 Bytes
MEAN = 127
SD = 127.50
MID = 127

*/
